[英]Qt can't connect to any slot manually (UI editor)
I am learning to use Qt. 我正在学习使用Qt。
For easier programming I use the UI-Editor. 为了简化编程,我使用UI编辑器。
THE PROBLEM 问题
When I want to connect my QTreeWidget, It suddenly doesn't want to do anything. 当我想连接我的QTreeWidget时,它突然不想做任何事情。
I don't want to connect it via UI-Editor, because I (later on) want to implement my own slot, which doesn't get recognized by the compiler (ERROR MESSAGE: didnt find slot ... in file ui_mainwindow.h
, although the slot is defined in mainwindow.cpp and declared in mainwindo.h) 我不想通过UI-Editor连接它,因为我(后来)想实现自己的插槽,编译器无法识别该 didnt find slot ... in file ui_mainwindow.h
(错误消息: didnt find slot ... in file ui_mainwindow.h
,尽管该插槽在mainwindow.cpp中定义并在mainwindo.h中声明)
So this is my connection code: 这是我的连接代码:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
//connects all the objects with the needed slots
QObject::connect(ui->treeWidget, &QTreeWidget::clicked, QApplication::quit);
ui->setupUi(this);
}
BUT: When I click on any of the items of QTreeWidget, it doesn't quit out of the programm. 但是:当我单击QTreeWidget的任何项目时,它都不会退出程序。
I am absolutely sure it's a QTreeWidget and not a QTreeView, I also use the Linux (-->GNOME) version of Qt 5.7 我绝对确定它是QTreeWidget而不是QTreeView,我也使用Qt 5.7的Linux(-> GNOME)版本
Also, I don't get any warnings/compiler errors. 另外,我没有收到任何警告/编译器错误。
Because I am a newb to Qt, I might have overseen a simple error, in this case, sorry :P 因为我是Qt的新手,所以我可能已经忽略了一个简单的错误,在这种情况下,对不起:P
Actually, there is no quit()
slot in QWidget
or its descendant QMainWindow
. 实际上, QWidget
或其后代QMainWindow
没有quit()
插槽。
You should use QCoreApplication::quit()
instead. 您应该改用QCoreApplication::quit()
。
Below, qApp
refers to the QApplication
instance: 下面, qApp
引用了QApplication
实例:
Qt 4 syntax: Qt 4语法:
QObject::connect(ui->treeWidget, SIGNAL(clicked(QModelIndex)), qApp, SLOT(quit()));
Qt 5 syntax: Qt 5语法:
QObject::connect(ui->treeWidget, &QTreeWidget::clicked, QCoreApplication::quit);
or 要么
QObject::connect(ui->treeWidget, &QTreeWidget::clicked, []{ QCoreApplication::quit(); });
Found the issue. 找到了问题。 Check the main.cpp
file, you are creating the QMainWindow
instance instead of MainWindow
. 检查main.cpp
文件,您正在创建QMainWindow
实例而不是MainWindow
。 That is why your derived class constructor isn't even called. 这就是为什么甚至没有调用派生类构造函数的原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.