提问:QT的 goto slot选项

Coutel 2016-07-30 01:20:45
RT,在学QT,讲师用QTCreator,在一次写例子的时候右键控件,使用了goto slot选项,但是我目前是VS+QT插件的模式,没用QTCreator

现在想知道goto slot实际所实现的代码是什么?

目前只知道:

比如右击一个button控件

会在 xx.h 头文件中的

private slots: 下增加一个空的button的槽函数,但是我根据需求写完该函数后运行程序,点击按钮并没有产生效果(该函数的代码和讲师的没有差异)。
而讲师的代码却具备功能。

所以我怀疑,是不是goto slot选项会在某个文件中自动补全一部分其他代码?
...全文
703 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
Coutel 2016-07-31
  • 打赏
  • 举报
回复
引用 9 楼 u013466477 的回复:
main函数应该这么写就可以了 int main(int argc,char *argv[]) { QApplication app(argc,argv); qt window; window->show(); return app.exec(); } on_button_clicked()槽函数要打括号 connect(ui.button, SIGNAL(clicked()), this, SLOT(on_button_clicked())); void qt::on_button_clicked() //槽函数 { qDebug()<<"------打印一句,看函数进来没---"; QProcess *run=new QProcess(); QString zifu1=ui.runline->text(); run->start(zifu1); }
感谢你对我的低级错误所进行的帮助。问题解决了.. 问题原因只是main函数中的 Ui_qtClass ui; ui.setupUi(window); 两句,或许是对象的实例化与调用setup ui函数和信号槽发生了什么冲突。 将不需要的这两行代码删除后 int main(int argc,char *argv[]) { QApplication app(argc,argv); qt *window=new qt(); window->show(); return app.exec(); } 其他部分代码不需要做改动即可完成功能。 最后还是感谢你的帮助,问题解决了,心里一块石头放下了..
冷静忍耐 2016-07-31
  • 打赏
  • 举报
回复
main函数应该这么写就可以了 int main(int argc,char *argv[]) { QApplication app(argc,argv); qt window; window->show(); return app.exec(); } on_button_clicked()槽函数要打括号 connect(ui.button, SIGNAL(clicked()), this, SLOT(on_button_clicked())); void qt::on_button_clicked() //槽函数 { qDebug()<<"------打印一句,看函数进来没---"; QProcess *run=new QProcess(); QString zifu1=ui.runline->text(); run->start(zifu1); }
Coutel 2016-07-31
  • 打赏
  • 举报
回复
引用 7 楼 u013466477 的回复:
不知道哪里崩溃了,按F5启动调试就只知道错在哪里了
我改加在了构造函数中,这次没报错,但是还是没有完成功能,我把各个文件的代码发出来,可以看看吗?谢谢 先是qt.h头文件: #ifndef QT_H #define QT_H #include <QtWidgets/QMainWindow> #include "ui_qt.h" #include <QProcess> class qt : public QMainWindow { Q_OBJECT public: qt(QWidget *parent = 0); ~qt(); private slots: void on_button_clicked(); //槽函数在这 private: Ui::qtClass ui; }; #endif // QT_H 然后是qt.cpp源文件 : #include "qt.h" #include "ui_qt.h" qt::qt(QWidget *parent) : QMainWindow(parent) { ui.setupUi(this); this->setMaximumSize(384,172); this->setMinimumSize(384,172); QObject::connect(ui.button, SIGNAL(clicked()), this, SLOT(on_button_clicked)); //连接在这 } qt::~qt() { } void qt::on_button_clicked() //槽函数的功能实现在这 { QProcess *run=new QProcess(); QString zifu1=ui.runline->text(); run->start(zifu1); } 最后是main.cpp源文件: #include <QWidget> #include <QApplication> #include <QString> #include <QDialog> #include <QLabel> #include <QHBoxLayout> #include <QVBoxLayout> #include <QLineEdit> #include <QPushButton> #include "qt.h" int main(int argc,char *argv[]) { QApplication app(argc,argv); qt *window=new qt(); Ui_qtClass ui; ui.setupUi(window); window->show(); return app.exec(); }
冷静忍耐 2016-07-30
  • 打赏
  • 举报
回复
不知道哪里崩溃了,按F5启动调试就只知道错在哪里了
冷静忍耐 2016-07-30
  • 打赏
  • 举报
回复
引用 4 楼 Coutel 的回复:
[quote=引用 3 楼 u013466477 的回复:] 因为没写connect connect(ui->button, SIGNAL(clicked()), this, SLOT(on_button_clicked()));
关于这个,我也想到了,不过这句连接应该加在哪里呢? 我写在了这里: int main(int argc,char *argv[]) { QApplication app(argc,argv); qt *window=new qt(); Ui_qtClass ui; ui.setupUi(window); window->show(); QObject::connect(ui.button, SIGNAL(clicked()), window, SLOT(on_button_clicked())); return app.exec(); } 下面是on_button_clicked()函数: void qt::on_button_clicked() { QProcess *run=new QProcess(); QString zifu1=ui.runline->text(); run->start(zifu1); } 但运行程序后点击按钮出现错误: 0x5269AD5B (Qt5Widgetsd.dll) (qt.exe 中)处有未经处理的异常: 0xC0000005: 读取位置 0x00000114 时发生访问冲突。 是我写的地方不对吗?[/quote] 应该是加在qt类构造函数的
Coutel 2016-07-30
  • 打赏
  • 举报
回复
引用 2 楼 QuantumEnergy 的回复:
另外要注意的是在你自定义一些槽函数的时候命名方式写成这样可能 会提示没有对应的信号,因为系统当成默认的方式,却又找不到对应的信号和widget ,当然你可以忽略这个提示,不会出问题,但最好的是命名方式不要和默认的规则样式一样
首先感谢你帮助,其次关于函数命名的问题 我无论是写成如 void xxxx(); 这样,还是void on_button_clicked();都完全不起作用。 另外在我看的那个QT教程中,讲师的代码部分连connect都没写,右键控件 goto slot, 然后只是把槽函数的实现部分写完了,然后直接编译运行就能达到效果。 而我则是写完函数后点击按钮无任何反应,写了connect后提示四楼所示的错误.. 附上地址: http://www.icoolxue.com/play/6668 丁林松的..... 很费解啊
Coutel 2016-07-30
  • 打赏
  • 举报
回复
引用 3 楼 u013466477 的回复:
因为没写connect connect(ui->button, SIGNAL(clicked()), this, SLOT(on_button_clicked()));
关于这个,我也想到了,不过这句连接应该加在哪里呢? 我写在了这里: int main(int argc,char *argv[]) { QApplication app(argc,argv); qt *window=new qt(); Ui_qtClass ui; ui.setupUi(window); window->show(); QObject::connect(ui.button, SIGNAL(clicked()), window, SLOT(on_button_clicked())); return app.exec(); } 下面是on_button_clicked()函数: void qt::on_button_clicked() { QProcess *run=new QProcess(); QString zifu1=ui.runline->text(); run->start(zifu1); } 但运行程序后点击按钮出现错误: 0x5269AD5B (Qt5Widgetsd.dll) (qt.exe 中)处有未经处理的异常: 0xC0000005: 读取位置 0x00000114 时发生访问冲突。 是我写的地方不对吗?
冷静忍耐 2016-07-30
  • 打赏
  • 举报
回复
因为没写connect connect(ui->button, SIGNAL(clicked()), this, SLOT(on_button_clicked()));
QuantumEnergy 2016-07-30
  • 打赏
  • 举报
回复
另外要注意的是在你自定义一些槽函数的时候命名方式写成这样可能 会提示没有对应的信号,因为系统当成默认的方式,却又找不到对应的信号和widget ,当然你可以忽略这个提示,不会出问题,但最好的是命名方式不要和默认的规则样式一样
QuantumEnergy 2016-07-30
  • 打赏
  • 举报
回复
因为在creator中生成的槽的命名方式为:void on_<widget name>_<signal name>(<signal parameters>); 系统会自动去找widget的这个对于那个 signal name 把它们连接起来,不需要用connect A Dialog With Auto-Connect Although it is easy to implement a custom slot in the dialog and connect it in the constructor, we could instead use QMetaObject's auto-connection facilities to connect the OK button's clicked() signal to a slot in our subclass. uic automatically generates code in the dialog's setupUi() function to do this, so we only need to declare and implement a slot with a name that follows a standard convention: void on_<widget name>_<signal name>(<signal parameters>); Using this convention, we can define and implement a slot that responds to mouse clicks on the OK button: class ImageDialog : public QDialog, private Ui::ImageDialog { Q_OBJECT public: ImageDialog(QWidget *parent = 0); private slots: void on_okButton_clicked(); }; Automatic connection of signals and slots provides both a standard naming convention and an explicit interface for widget designers to work to. By providing source code that implements a given interface, user interface designers can check that their designs actually work without having to write code themselves.

16,239

社区成员

发帖
与我相关
我的任务
社区描述
Qt 是一个跨平台应用程序框架。通过使用 Qt,您可以一次性开发应用程序和用户界面,然后将其部署到多个桌面和嵌入式操作系统,而无需重复编写源代码。
社区管理员
  • Qt
  • 亭台六七座
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧