Results 1 to 2 of 2

Thread: how to display dialog message

  1. #1
    Join Date
    Jan 2010
    Posts
    63
    Qt products
    Qt4
    Platforms
    Windows

    Unhappy how to display dialog message

    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 ?

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: how to display dialog message

    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.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

Similar Threads

  1. Replies: 3
    Last Post: 29th December 2008, 16:54
  2. I need to send a message to parent dialog
    By santhoshv84 in forum Qt Programming
    Replies: 1
    Last Post: 3rd July 2008, 11:16
  3. why QFileDialog does not display the default winxp dialog?
    By mismael85 in forum Qt Programming
    Replies: 5
    Last Post: 21st March 2008, 13:39
  4. Replies: 1
    Last Post: 7th November 2007, 08:39
  5. Replies: 11
    Last Post: 5th June 2007, 09:06

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.