0

Qt を使用して、いくつかの基本的なビジュアル アプリケーション用に C++ を学習しようとしています。ボタンを押すとどこかにテキストメッセージを表示したい。これが私のコードです:

main.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

main.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 メッセージにあるかどうかを知るために、ファイルにテキストを出力する必要があるコードをいくつか入れましたが、メンバー関数は実行されません。

誰でも私を助けることができますか?

ありがとうございました。

4

3 に答える 3

3

コードを機能させるために、コードを変更する必要があったことが 3 つあります。

まず、main.h で Q_OBJECT マクロを使用する必要があります。

class MyApp : public QWidget {
   Q_OBJECT

this次に、main.cxx で、接続呼び出しを正しいレシーバー (ではなくmyApp)に変更する必要があります。

connect(show_msg, SIGNAL(clicked()), this, SLOT(showIt()));

3 番目に、main.cxx でmessage、クラス メンバーとしてラベルを作成するコードのコメントを解除する必要があります。

message = new QLabel(this);
于 2012-12-04T17:55:12.493 に答える
2

最も重要な部分は、次のように信号を特定のオブジェクトのスロットに接続することです。

connect(object_emitting, SIGNAL(clicked()),object_receiving, SLOT(showIt()));

于 2012-12-05T02:03:06.270 に答える
0

connect(show_msg, SIGNAL(clicked()), qApp, SLOT(showIt()));この染みを に変更qAppしてthis、もう一度やり直してください

つまり

 connect(show_msg, SIGNAL(clicked()), this, SLOT(showIt())); 

privat slots:との間に違いがあるかどうかはprotected slots:わかりませんが、通常privat slots:は変更して試すこともできます:)

于 2012-12-04T17:50:19.000 に答える