PDA

View Full Version : how to display dialog message



Petr_Kropotkin
12th January 2010, 14:59
I am reading C++-GUI-Programming-with-Qt-4-1st-ed.pdf and trying to modify the code to see how far I can take each lesson.
I want a message window to popup when ten spinbox hits 23. Code in bold is mine.
Here is base code
#include <QApplication>
#include <QHBoxLayout>
#include <QSlider>
#include <QSpinBox>
#include <QLabel>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);

QWidget *window = new QWidget;
window->setWindowTitle("Enter Your Age");

QSpinBox *spinBox = new QSpinBox;
QSlider *slider = new QSlider(Qt::Horizontal);

QLabel *messagel = new QLabel("You are 23 !");

spinBox->setRange(0, 130);
slider->setRange(0, 130);
QObject::connect(spinBox, SIGNAL(valueChanged(int)), slider, SLOT(setValue(int)));
QObject::connect(slider, SIGNAL(valueChanged(int)), spinBox, SLOT(setValue(int)));
spinBox->setValue(35);

QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget(spinBox);
layout->addWidget(slider);
window->setLayout(layout);
window->show();
return app.exec();

}

I tried everything.... I tried something like this:

QObject::connect(spinbox, SIGNAL(setValue(int)), message, SLOT(setValue(int)));
or
QObject::connect(spinbox, SIGNAL(setValue(int)), message, SLOT(message->display()));
or
if (spinbox->value()==23)
{ message->display()}

It displays the message but not when the slider or spinbox hits 23 .:(
Can someone show me the code to insert to make it work ?

high_flyer
12th January 2010, 15:16
1. setValue() is a slot, not a signal. (as you tried at the end of your post)
2. if you are connecting to a signal, you want that signal to trigger a slot that does what you want.
In your code you are connecting the QSpinBox signals to its own slots, which probably creates a recursive behavior.

3. Since no Qt class has a slot that does what you want (popping a message box with the text "You are 23 !") you will have to subclass a QWidget or one of its subclasses, and implement that slot in that subclass.

Then you can connect the spin box signal valueChanged() to that slot.

P.S
Please use the code tags for posting code.