无法在Qt中制作信号和插槽
Can't make Signals and Slots works in Qt
我正在尝试学习C++使用Qt进行一些基本的视觉应用程序。我想在开始时做一些简单的事情,当我按下按钮时,我想在某处显示一条短信。这是我的代码:
主H
#ifndef MYAPP_H
#define MYAPP_H
#include <QWidget>
class QLabel;
class QString;
class MyApp : public QWidget{
public:
MyApp(QWidget *parent = 0);
protected slots:
void showIt();
private:
QString *text_msg;
QLabel *message;
};
#endif
主.cpp
#include <QApplication>
#include <QFont>
#include <QPushButton>
#include <QWidget>
#include <QLabel>
#include <QVBoxLayout>
#include <QFrame>
#include <QString>
#include <vector>
#include <string>
#include <map>
#include "main.h"
#include <fstream>
using std::map;
using std::vector;
using std::string;
using std::ofstream;
/* implementation */
MyApp::MyApp(QWidget *parent):QWidget(parent){
QString text_msg;
text_msg = "This is my first message written in C++! n It was printed with Qt!";
setFixedSize(400, 280);
QPushButton *quit = new QPushButton(tr("Quit"), this);
quit->setGeometry(62, 40, 75, 50);
quit->setFont(QFont("Times", 18, QFont::Bold));
QPushButton *show_msg = new QPushButton(tr("Show!"), this);
show_msg->setGeometry(30,15,75,45);
show_msg->setFont(QFont("Times", 18, QFont::Bold));
//message = new QLabel();
QLabel *message = new QLabel(this);
//message->setFrameStyle(QFrame::Panel | QFrame::Sunken);
//message->setText(text_msg);
//message->setText("asdf");
connect(quit, SIGNAL(clicked()), qApp, SLOT(quit()));
connect(show_msg, SIGNAL(clicked()), qApp, SLOT(showIt()));
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(message);
layout->addWidget(show_msg);
layout->addWidget(quit);
setLayout(layout);
ofstream myfile;
myfile.open ("example");
myfile << "Writing this to a file.n";
myfile.close();
}
void MyApp::showIt(){
//*text_msg = "xyz";
ofstream myfile;
myfile.open ("example");
myfile << "12121212121.n";
myfile.close();
message->setText("1234");
}
int main(int argc, char *argv[])
{
/* assign messages for output
bool result;
string key = "first", message="this is a sample text";
result = Messages::add_message(key, message);
*/
/* end */
QApplication app(argc, argv);
MyApp my_simple_app;
my_simple_app.show();
return app.exec();
}
我不明白为什么程序不运行插槽成员功能。我放了一些代码,应该在文件中打印一些文本,以了解该函数中的代码是否会被执行,问题出在 QLabel 消息上,但成员函数没有执行。
有人可以帮助我吗?
谢谢。
我需要对您的代码进行更改 3 件事才能使其正常工作:
首先,在main.h中,您需要使用Q_OBJECT宏:
class MyApp : public QWidget {
Q_OBJECT
其次,在main.cxx中,您需要将连接调用更改为正确的接收器(this
而不是myApp
):
connect(show_msg, SIGNAL(clicked()), this, SLOT(showIt()));
第三,在 main.cxx 中,您需要取消注释将message
标签创建为类成员的代码:
message = new QLabel(this);
最重要的部分是将信号连接到给定对象的插槽,如下所示:
connect(object_emitting, SIGNAL(clicked()),object_receiving, SLOT(showIt()));
connect(show_msg, SIGNAL(clicked()), qApp, SLOT(showIt()));
在此染色中更改qApp
this
并重试
这意味着
connect(show_msg, SIGNAL(clicked()), this, SLOT(showIt()));
如果privat slots:
和protected slots:
之间有所不同,我不会但我经常使用privat slots:
您也可以更改它并尝试:)
相关文章:
- 控制带有信号/插槽的Qt QML滑动视图
- Qt 信号/插槽问题
- Qt moveToThread,带有参数的信号/插槽
- moc 文件中缺少信号插槽
- 文档在哪里说明如果参数不是从末端删除参数,则无法从QT信号插槽连接中删除参数?
- 信号/插槽多线程 Qt
- 通过排队的QT信号插槽连接发送QSHAREDPOINTERS的QVECTOR
- Qimage没有通过信号插槽QT
- 使用 Qt5 的新信号/插槽实现向滑块发出信号
- 一个类中的QT信号/插槽,但从不同的线程发出
- 信号/插槽基类多继承
- 在QT C 中使用QMOVIE和信号/插槽之间在GIF中导航
- 在 for 循环中使用 lambda 函数连接信号插槽
- 不同类别的 2 个对象之间的信号/插槽
- 如何正确使用qt的信号/插槽系统
- Qt信号插槽视觉工作室:似乎未连接
- QACTION信号插槽中的无形UI
- Qt-连接触发动作中的信号/插槽
- Qt 信号插槽类范围类型
- 信号/插槽 Qt5 C++