无法在Qt中制作信号和插槽

Can't make Signals and Slots works in Qt

本文关键字:信号 插槽 Qt      更新时间:2023-10-16

我正在尝试学习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:您也可以更改它并尝试:)