gpt4 book ai didi

c++ - Qt - 强制关闭按钮点击(信号/插槽问题)

转载 作者:行者123 更新时间:2023-11-30 03:09:17 24 4
gpt4 key购买 nike

不确定为什么这不起作用,但是 connect(button, SIGNAL(clicked()), this, SLOT(addMenu())); 当我点击按钮。

主视图.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

主视图.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);

}

最佳答案

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

这段代码:

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

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

关于c++ - Qt - 强制关闭按钮点击(信号/插槽问题),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4227983/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com