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;
box->setLayout(layout);

// Textbox
QTextEdit *te = new QTextEdit;
layout->addWidget(te);
te->setHtml("");

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

layout->addWidget(adviceButton);
layout->addWidget(weatherButton);
layout->addWidget(nextmeetingButton);
layout->addWidget(quitButton);

//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;
}



#include
#include
#include
#include
#include
#include
#include

class Messager : public QObject
{
Q_OBJECT

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

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

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




#include "messager.h"

//====================================

Comments

  • 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):

    [code]
    #include "MyDialog.h"

    int main()
    {
    QApplication a(argc, argv);
    MyDialog w;
    w.show();
    return a.exec();
    }
    [/code]

    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!

Categories

In this Discussion