Results 1 to 20 of 20

Thread: send messages from central widget to mainwindow (statusBar)??

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Nov 2010
    Posts
    142
    Thanks
    24
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default send messages from central widget to mainwindow (statusBar)??

    • I have created a mainwindow with a status bar
    • then i created a widget
    • i set that widget as the central widget
    • now, I added several other widgets in that widget.

    The question how I can send some text messages from those widgets to the mainwindow status bar?!

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: send messages from central widget to mainwindow (statusBar)??

    Connect to the QStatusBar::showMessage() slot and send away.

  3. #3
    Join Date
    Nov 2010
    Posts
    142
    Thanks
    24
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: send messages from central widget to mainwindow (statusBar)??

    well, it seems obvious, but I have something missing (probably important block of knowledge) here:
    I have successfully constructed a status bar in the mainwindow:

    Qt Code:
    1. mainwindow.h
    2. public slots:
    3. void printInfoToStatusBar (QString);
    4. mainwindow.cpp
    5. void MainWindow::printInfoToStatusBar (QString msg)
    6. {
    7. statusBar()->showMessage(msg);
    8. }
    To copy to clipboard, switch view to plain text mode 

    I have a central widget and in the central widget I have added a widget with a slidebar. I want to see the values of the slidebar in the statusbar. How will i connect them? (i have more messages to send to the mainwindow, but if I get this, I will do the rest similaringly):


    Qt Code:
    1. connect( myslider, SIGNAL( valueChangedinQStringType(QString) ), ???mainwindow??, SLOT( printInfoToStatusBar (QString ) ) );
    To copy to clipboard, switch view to plain text mode 

    PS: I have seen some examples of people using ui->this,etc,etc, but the way I started building my application had no ui whatsoever and now I cannot change the constructor of the main window (the ui way).

  4. #4
    Join Date
    Jan 2009
    Location
    The Netherlands and Spain
    Posts
    150
    Thanks
    6
    Thanked 18 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: send messages from central widget to mainwindow (statusBar)??

    Qt Code:
    1. in mainwindow.h
    2.  
    3. private slots:
    4. void mySliderChanged(int);
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. in MainWindow.cpp
    2.  
    3. in constructor MainWindow::MainWindow
    4. connect(mySlider, SIGNAL(valueChanged(int)), this, SLOT(mySliderChanged(int)));
    5.  
    6. void MainWindow::mySliderChanged(int newValue)
    7. {
    8. ... send something to your statusbar ...
    9. }
    To copy to clipboard, switch view to plain text mode 

    In some cases you can send a value directly from one control to another, but processing a signal the way shown above, gives you freedom to change values or formats before sending it to someplace else.

    Make sure that mySlider has tracking == true, if you want immidiate update when moving the slider:
    Qt Code:
    1. mySlider.setTracking(true);
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Nov 2010
    Posts
    142
    Thanks
    24
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: send messages from central widget to mainwindow (statusBar)??

    @boudie
    the point is that mySlider does not belong to "MainWindow" class, but to a widget (central) of the MainWindow.That means that:

    Qt Code:
    1. constructor MainWindow::MainWindow
    2. connect(mySlider, SIGNAL(valueChanged(int)), this, SLOT(mySliderChanged(int)));
    To copy to clipboard, switch view to plain text mode 

    cannot be used

  6. #6
    Join Date
    Jan 2009
    Location
    The Netherlands and Spain
    Posts
    150
    Thanks
    6
    Thanked 18 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: send messages from central widget to mainwindow (statusBar)??

    How and where did you create mySlider?
    Please show complete code.

  7. #7
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: send messages from central widget to mainwindow (statusBar)??

    If the slider is part of a Designer built QMainWindow, i.e. the central widget sub-widgets are accessible to the main window, then something like this will work:
    Qt Code:
    1. connect(ui->mySlider, SIGNAL(valueChanged(int)), this, SLOT(mySliderChanged(int)));
    To copy to clipboard, switch view to plain text mode 

    If you have a completely separate widget (OtherWidget) that you set as the central widget then its sub-widgets are not usually accessible from outside. In this case you should expose a signal in class OtherWidget that simply repeats the slider's valueChanged(int) signal to the outside world. Then you connect that externally visible signal as above. Something like:
    Qt Code:
    1. class OtherWidget: public QWidget {
    2. Q_OBJECT
    3. public:
    4. OtherWidget() {
    5. mySlider = new QSlider(this);
    6. ...
    7. connect(mySlider, SIGNAL(valueChanged(int)), this, SIGNAL(sliderChanged(int)));
    8. }
    9. signals:
    10. void sliderChanged(int value);
    11. private:
    12. QSlider *mySlider;
    13. };
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Nov 2010
    Posts
    142
    Thanks
    24
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: send messages from central widget to mainwindow (statusBar)??

    thanks ChrisW67 ,
    I am close to understand, but I am still a bit confused. After further reading, here's what I have got:

    mainwindow.h
    Qt Code:
    1. public slots:
    2. void printInfoToStatusBar (QString);
    To copy to clipboard, switch view to plain text mode 
    mainwindow.cpp
    Qt Code:
    1. void MainWindow::printInfoToStatusBar (QString msg)
    2. {
    3. statusBar()->showMessage(msg);
    4. }
    To copy to clipboard, switch view to plain text mode 

    then indeed OtherWidget is the central widget of the main window
    OtherWidget.h
    Qt Code:
    1. class OtherWidget : public QWidget
    2. {
    3. public:
    4. OtherWidget(QWidget *parent = 0);
    5. signals:
    6. void sliderChanged(int value);
    7. private:
    8. .....
    9. ........other widgets....
    10. QSlider mySlider;
    11. };
    To copy to clipboard, switch view to plain text mode 

    OtherWidget.cpp
    Qt Code:
    1. OtherWidget::OtherWidget(QWidget *parent)
    2. : QWidget(parent)
    3. {
    4. mySlider = new QSlider(this);
    5. connect(mySlider, SIGNAL(valueChanged(int)), this, SIGNAL(sliderChanged(int)));//connecting two SIGNALS, right?
    6. }
    7.  
    8. void OtherWidget::sliderChanged(int)
    9. {
    10. //is this the place that I should reflect the slider signal to the mainwindow?how?
    11. }
    To copy to clipboard, switch view to plain text mode 


    PS: I haven't used Designer, just Creator.
    Last edited by fatecasino; 23rd December 2010 at 23:13. Reason: reformatted to look better

  9. #9
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: send messages from central widget to mainwindow (statusBar)??

    Yes, connecting two SIGNALS together. When the source signal is emitted the destination signal will be emitted. It is in the docs.

    As you have it now, you need to find the correct spot to:
    Qt Code:
    1. connect(
    2. otherWidget, SIGNAL(sliderChanged(int)),
    3. mainWindow, SLOT(printInfoToStatusBar(QString)) // the function templates do not match: you'll need to sort this out
    4. );
    To copy to clipboard, switch view to plain text mode 
    Probably immediately after creating an instance of OtherWidget.

    You could redefine the signal out of OtherWidget:
    Qt Code:
    1. public slots:
    2. void sliderChanged(int value);
    3. signals:
    4. void statusMessage(QString msg);
    5. ...
    6. connect(mySlider, SIGNAL(valueChanged(int)), this, SLOT(sliderChanged(int)));
    7. ...
    8. void OtherWidget::sliderChanged(int value)
    9. {
    10. emit statusMessage( QString("Some status message including the %1").arg(value) );
    11. }
    To copy to clipboard, switch view to plain text mode 
    or redefine the slot in the main window class to accept an int.

  10. The following user says thank you to ChrisW67 for this useful post:

    fatecasino (26th December 2010)

Similar Threads

  1. Replies: 5
    Last Post: 30th November 2010, 06:40
  2. Qt Designer About central widget in Qt Designer
    By lrjdragon in forum Qt Tools
    Replies: 2
    Last Post: 3rd April 2010, 16:11
  3. how to send broadcast messages in networkadapters
    By jeo in forum Qt Programming
    Replies: 3
    Last Post: 26th March 2009, 10:17
  4. QDockWidgets without central widget
    By JoeMerchant in forum Qt Programming
    Replies: 17
    Last Post: 8th August 2007, 14:52
  5. Central Widget of QMainWindow
    By sumsin in forum Qt Programming
    Replies: 3
    Last Post: 13th March 2006, 18:32

Tags for this Thread

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.