C++ slots qt4 : Create an app with four buttons? Here is my code ...

Okay i have been given the code? But which ones are the headers and which ones are the main.cpp - in other words how can i make this execute?

in other words how do i save then correctly to make the program run?

button 1 = "Advice" , should be connected to a slot that randomly selects a piece of text (such as a fortune cookie) and displays it in the QTextEdit window.

button 2 = "Weather", randomly selects a sentence about weather and displays it in the QTextEdit window.

button 3 = "Next Meeting" , pops ups a message dialog with a randomly generated (fictitious) meeting time and descriptive message in it.

button 4 = "Quit" , terminates the program.

Have to use signals and slots to connect the button clicks with appropriate functions.

QWidget* setGui(QWidget *box)
QLayout* layout = new QVBoxLayout;

// Textbox
QTextEdit *te = new QTextEdit;

// Buttons
QPushButton *adviceButton = new QPushButton("Advice");
QPushButton *weatherButton = new QPushButton("Weather");
QPushButton *nextmeetingButton = new QPushButton("Next Meeting");
QPushButton *quitButton = new QPushButton("Quit");


//Tie buttons to slots
Messager *mymsg = new Messager(te, box);
qApp->connect(adviceButton, SIGNAL(clicked()), mymsg, SLOT(advice()));
qApp->connect(weatherButton, SIGNAL(clicked()), mymsg, SLOT(weather()));
qApp->connect(nextmeetingButton, SIGNAL(clicked()), mymsg, SLOT(nextmeeting()));

QObject::connect(quitButton, SIGNAL(clicked()), qApp, SLOT(quit()));
return box;


class Messager : public QObject

Messager(QTextEdit* te, QWidget* parent=0);

public slots:
void advice();
void weather();
void nextmeeting();

QWidget* m_Parent;
QErrorMessage* message;
QTextEdit* m_te;
QList myWeatherList;
QList myAdviceList;
QList myMeetingList;

#include "messager.h"



  • I think your problem is: 'I have create a Qt class, how do I use/display it?'

    1) Add a file called 'main.cpp' (for example)

    2) Write the code (and replace 'MyDialog' by your class name):

    #include "MyDialog.h"

    int main()
    QApplication a(argc, argv);
    MyDialog w;
    return a.exec();

    Good luck!
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


In this Discussion