Reputation: 1788
I'm trying to learn C++ using Qt for some basic visual applications. I want to make something simple at start, when I push a button, I want to display a text message somewhere. Here is my code:
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();
}
I don't understand why the program doesn't run the slot member function. I put there a some code that should print in a file some text to know if the code inside that function will be executed and the problem is at the QLabel message, but the member function are not executed.
Anyone can help me?
Thank you.
Upvotes: 0
Views: 271
Reputation: 21
The most important part is connecting the signal to the slot for a given object like this:
connect(object_emitting, SIGNAL(clicked()),object_receiving, SLOT(showIt()));
Upvotes: 2
Reputation: 876
connect(show_msg, SIGNAL(clicked()), qApp, SLOT(showIt()));
in this stainment change qApp
to this
and try again
that means
connect(show_msg, SIGNAL(clicked()), this, SLOT(showIt()));
im not suer if there is different between privat slots:
and protected slots:
but usuely I use privat slots:
also u can change it and try :)
Upvotes: 0
Reputation: 7777
There are 3 things that I needed to change to your code to make it work:
Firstly, in main.h you need to use the Q_OBJECT macro:
class MyApp : public QWidget {
Q_OBJECT
Secondly, in main.cxx, you need to change the connect call to the correct receiver (this
instead of myApp
):
connect(show_msg, SIGNAL(clicked()), this, SLOT(showIt()));
Thirdly, in main.cxx, you need to uncomment the code that creates the message
label as a class member:
message = new QLabel(this);
Upvotes: 3