Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3398858
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T04:42:01+00:00 2026-05-18T04:42:01+00:00

Not sure why this isn’t working, but connect(button, SIGNAL(clicked()), this, SLOT(addMenu())); force closes the

  • 0

Not sure why this isn’t working, but connect(button, SIGNAL(clicked()), this, SLOT(addMenu())); force closes the application when I click on the button.

mainview.h

#ifndef MAINVIEW_H
#define MAINVIEW_H

#include <QtGui/QMainWindow>
#include <QtGui/QScrollArea>
#include <QtGui/QFrame>
#include <QtGui/QVBoxLayout>
#include <QtGui/QPushButton>
#include <QtGui/QLabel>
#include <QtGui/QLineEdit>
#include <QtGui/QGroupBox>
#include <QtGui/QFormLayout>
#include <QtGui/QMessageBox>

#include <QtCore/QPointer>
#include <QtCore/QFile>
#include <QtCore/QIODevice>
#include <QtCore/QList>
#include <QtCore/QMap>
#include <QtCore/QString>

#include <QtXml/QXmlStreamReader>
#include <QtDebug>
#include <QBool>
#include <QSignalMapper>
#include <QStackedLayout>
#include <QSize>
#include <QPalette>

//
// MainView
//

class MainView: public QMainWindow {

    Q_OBJECT

public:
    MainView(QWidget *parent = 0);
    ~MainView();

private:
    void buildTabMenuBar(int index);

public slots:
    void activeTabChanged(int index);

signals:

private:
    QTabWidget* tabWidget;

};

//
// Tab1
//

class QMyWidget1: public QWidget {

    Q_OBJECT

public:
    QMyWidget1(QWidget *parent = 0);
    ~QMyWidget1();

public slots:
    void runOnTabSelect();
    void addMenu();

signals:

private:
    QPointer<QVBoxLayout> _layout;
    QPointer<QVBoxLayout> _layoutToAdd;

};

#endif // MAINVIEW_H

mainview.cpp

#include "mainview.h"

#include <QtGui>
#include <QApplication>
#include <QPixmap>
#include <QSize>

//
// Tabs Main
//

MainView::MainView(QWidget *parent) : QMainWindow(parent) {

    setContextMenuPolicy(Qt::NoContextMenu);
    this->setWindowTitle("Main");

    //create tabwidget
    tabWidget = new QTabWidget(this);

    //tab changes
    QObject::connect(tabWidget, SIGNAL(currentChanged(int)),this, SLOT(activeTabChanged(int)));

    //tab 1
    QMyWidget1* widget1 = new QMyWidget1();

    QScrollArea* scroll1 = new QScrollArea();
    scroll1->setBackgroundRole(QPalette::Dark);
    scroll1->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
    scroll1->setWidget(widget1);

    tabWidget->addTab(scroll1, "Tab1"); 

    //create stacked layout
    QStackedLayout *stackedLayout = new QStackedLayout;

    //add widgets to stack
    stackedLayout->addWidget(tabWidget);

    //make fullscreen eventually, won't do this automatically
    stackedLayout->setGeometry(QRect(0,0,350,600));

    QVBoxLayout *_layout = new QVBoxLayout;
    _layout->addLayout(stackedLayout);
    setLayout(_layout);

    //remove context menu from all widgets
#ifdef Q_OS_SYMBIAN
    QWidgetList widgets = QApplication::allWidgets();
    QWidget* w = 0;
    foreach(w,widgets) {

            w->setContextMenuPolicy(Qt::NoContextMenu);

    }
#endif
}

MainView::~MainView() {

}

void MainView::activeTabChanged(int index) {

    //active tab
    buildTabMenuBar(index);

}

void MainView::buildTabMenuBar(int index) {

    //clear current menu
    QMenuBar* menubar = menuBar();
    menubar->clear();

    //build new menu into active tab
    switch (index) {
    case 0:
    {
        menubar->addAction("", tabWidget->widget(index), SLOT(runOnTabSelect()));
        break;
    }
    default:
    {
        break;
    }
    };
}

//
// Tab1 Content
//

QMyWidget1::QMyWidget1(QWidget *parent) : QWidget(parent) {

    setContextMenuPolicy(Qt::NoContextMenu);

    QVBoxLayout* _layout = new QVBoxLayout(this);

    //make button

    QPushButton* button = new QPushButton("Just a button");
    button->setStyleSheet("QPushButton { border-left: 1px solid white; border-right: 1px solid white; border-top: 1px solid white; border-bottom:none; border-radius: 3px; background-color: #404040; width: 350px; height:90px; font-size: 15px; font-family: georgia, garamond, serif; margin-bottom:3px; } QPushButton:pressed { background-color: white; }");

    button->setIcon(QIcon("c://right_arrow.png"));
    button->setLayoutDirection(Qt::LeftToRight);
    button->setIconSize(QSize(32,32));
    _layout->addWidget(button);

    **connect(button, SIGNAL(clicked()), this, SLOT(addMenu()));**

    this->setLayout(_layout);

}

QMyWidget1::~QMyWidget1() {

}

void QMyWidget1::runOnTabSelect() {

}

void QMyWidget1::addMenu() {

    _layoutToAdd = new QVBoxLayout;

    QPushButton* button = new QPushButton("New Button");
    _layoutToAdd->addWidget(button);

    _layout->insertLayout(0, _layoutToAdd, 0);

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T04:42:02+00:00Added an answer on May 18, 2026 at 4:42 am

    Your member variable _layout is apparently not initialized.

    This code:

     QVBoxLayout *_layout = new QVBoxLayout;
    _layout->addLayout(stackedLayout);
    setLayout(_layout);
    

    creates a local variable _layout, which is different from the member variable _layout.
    As you’re using a QPointer for _layout (the member) it’s initialized to NULL, which then leads to a crash in your addMenu() slot.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm not sure why this isn't working. I don't have any errors, but what
Not sure why this isn't working. When people click the 'edit' button of my
I'm not sure why this isn't working but probably because I'm not very familiar
I'm not entirely sure why this isn't working. But this piece of code does
I'm not sure why this isn't working: import re import csv def check(q, s):
not sure why this isn't working-- i have 3 divs where the two on
I'm not sure why this isn't working. I have a record list of text
Not sure why this isn't working... I have several DIVs with an class of
Not sure why this isn't working. It was working perfectly and then I restarted
Not sure why this isn't working. I have a timer defined, which runs a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.