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(th开发者_Go百科is);
//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);
}
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.
First step, is it the click or the function
1, Temporarily replace the contents of the function with an empty one.
2, Then work out exactly which call in the function is crashing - do you have a debugger?
精彩评论