Qt - 按下按钮强制关闭(信号/插槽问题)

时间:2010-11-19 18:02:53

标签: c++ qt qt4

不确定为什么这不起作用,但连接(按钮,SIGNAL(clicked()),这,SLOT(addMenu())); 强制关闭应用程序,当我点击按钮。

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);

}

2 个答案:

答案 0 :(得分:3)

您的成员变量_layout显然未初始化。

此代码:

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

创建一个本地变量_layout,它与成员变量_layout不同。 当您使用QPointer for _layout(成员)时,它会初始化为NULL,然后导致addMenu()插槽崩溃。

答案 1 :(得分:0)

第一步,是点击还是功能

1,暂时用空的内容替换该功能的内容。

2,然后确定函数中的哪个调用正在崩溃 - 你有调试器吗?