Results 1 to 20 of 20

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

  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)

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

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

    Hi Chris,

    I think I am getting closer, but I am stuck with this error:

    Qt Code:
    1. error: expected primary-expression before ‘,’ token
    To copy to clipboard, switch view to plain text mode 

    which means that I am probably doing something very wrong connecting signal/slot.

    In the constructor of otherWidget (I call it MyWidget)

    Qt Code:
    1. mySlider = new QSlider;
    2. //connect(mySlider, SIGNAL(valueChanged(int)), this, SIGNAL( sendInfoToMainWindows(int) ));
    3. connect(mySlider, SIGNAL(valueChanged(int)), this, SLOT( sendInfoToMainWindows(int) ));
    4.  
    5. connect( MyWidget, SIGNAL( sendInfoToMainWindows(int) ), MainWindow, SLOT( printNumberInfoToStatusBar(int) ) );//error!!!
    To copy to clipboard, switch view to plain text mode 

    I tried without success to put the connection line in the MainWindow.cpp
    Qt Code:
    1. widget = new MyWidget;
    2. setCentralWidget(widget);
    3. connect( widget, SIGNAL( sendInfoToMainWindows(int) ), MainWindow, SLOT( printNumberInfoToStatusBar(int) ) );
    To copy to clipboard, switch view to plain text mode 


    I know the emit function is wrong, but I just want to emit the value that the slider sends to the MyWidget

    Qt Code:
    1. void MyWidget::sendInfoToMainWindows(int value)
    2. {
    3. emit value;
    4. }
    To copy to clipboard, switch view to plain text mode 


    I sorted out the mainWindow message function to accept integers:

    Qt Code:
    1. void MainWindow::printNumberInfoToStatusBar (int value)
    2. {
    3. QString str;
    4. str.setNum(value);
    5. statusBar()->showMessage(str);
    6. }
    To copy to clipboard, switch view to plain text mode 


    Any ideas?~!


    Added after 10 minutes:


    well, it is this magic effect when you press the post button of the forum, you get a new idea and things work better.

    The connection SHOULD be in the MainWindow.cpp

    Qt Code:
    1. widget = new MyWidget;
    2. setCentralWidget(widget);
    3. connect( widget, SIGNAL( sendInfoToMainWindows(int) ), this, SLOT( printNumberInfoToStatusBar(int) ) );
    To copy to clipboard, switch view to plain text mode 

    everything compiles, but now I have to find the right syntax to emit the value that the MYWidget gets from the slide to the MainWindow

    Qt Code:
    1. void MyWidget::sendInfoToMainWindows(int value)
    2. {
    3. emit value;
    4. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by fatecasino; 26th December 2010 at 16:59.

  12. #11
    Join Date
    Dec 2010
    Location
    Russia
    Posts
    83
    Thanks
    1
    Thanked 12 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

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

    Hey there!

    1.Connect MyWidget's widgets with MyWidget's signal (as Chris said,you just want to propogate your inner signals):

    connect(mySlider, SIGNAL(valueChanged(int)), SIGNAL( sendInfoToMainWindows(int) )); //you may not write this keyword

    2.As you said,MyWidget is not initially aggregated by MainWindow.But there's got to be a place in your program where you actually invoke QMainWindow::setCentralWidget method.Let's say you do this in your main.cpp,your main function may look like this:

    int main(int argc,char* argv[]){

    QApplication app(argc,argv);

    MainWindow mainWin;

    MyWidget widget;

    mainWin.setCentralWidget(&widget);

    QObject::connect(&widget,SIGNAL(sendInfoToMainWindows(int)),&mainWin,SLOT(printNumberInfoToStatusBar(int)));

    mainWin.show();

    return app.exec();


    }

    That's it,you have your widgets connected now...By the way, you can have a QString contructed from the int type (and other built-in types) via the static method:

    QString number ( int n, int base = 10 )

    So check out QAssistant's page on QString

    Hope you have an idea now...

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

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

    supposing I have successfully connected the widgets to the mainWindow in the constructor of the Mainwindow.cpp, I cannot figure out how I will emit the int signal:

    Qt Code:
    1. void MyWidget::sendInfoToMainWindows(int value)
    2. {
    3. emit value;
    4. }
    To copy to clipboard, switch view to plain text mode 

  14. #13
    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)??

    Take a look back at post #9 in this thread. You need to name the signal you are emitting and provide it with any arguments it requires. The word "emit" is syntactic sugar, the rest of the line is essentially a function call.

    This will all make more sense it you read the "Signals and slots" information available from the "Qt Object Model" page in Assistant.

  15. #14
    Join Date
    Dec 2010
    Location
    Russia
    Posts
    83
    Thanks
    1
    Thanked 12 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

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

    Think about connecting signal to signal like this: every time one is emitted,another is emitted as well...tyou don't have to emit connected signals manually (if it wasn't,what's the point of connections then)

  16. #15
    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, I have been studying several signal/slot examples and there is always a dark area in my understanding:

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

    The function valueChanged(int) function, is a signal function.
    It sends the actual value of the slider to the MyWidget.
    Then,
    The function sendInfoToMainWindows(int) sends this signal from the MyWidget to the MainWIndow.
    In simple words valueChanged(int) and sendInfoToMainWindows(int) do a very similar job, right? they emit a signal to a certain destination.
    Question:
    What is the exact source code of the valueChanged(int) so I can reproduce it for the sendInfoToMainWindows(int)?


    @AlexSudnik
    I think I am trying to do exactly the thing you describe.

    The slider sends a value signal to the central widget
    Qt Code:
    1. connect(mySlider, SIGNAL(valueChanged(int)), this, SIGNAL( sendInfoToMainWindows(int) ));
    To copy to clipboard, switch view to plain text mode 


    the sendInfoToMainWindows(int) receives it and resends(?) it to the printNumberInfoToStatusBar(int) of the MainWindow
    Qt Code:
    1. connect( widget, SIGNAL( sendInfoToMainWindows(int) ), this, SLOT( printNumberInfoToStatusBar(int) ) );
    To copy to clipboard, switch view to plain text mode 

    I believe that the actual missing part is the source code of the sendInfoToMainWindows(int) because I have never designed a signal function so far.

  17. #16
    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)??

    The main points:
    • Want the signal from the slider on your custom widget to be connected to the slot of the main window status bar.
    • Cannot do this directly because the slider on your widget is private (in the C++ sense) to the widget.
    • Need to expose a signal in the public interface of the widget so that it can be connected to the status bar slot.
    • The signal should fire every time the signal of the widget slider fires.
    • The signal needs to match the integer argument from the slider signal to the string argument expected by the slot.
    • There is more than one way to do it.


    One way to organise it:
    • Private slider's valueChanged(int) signal
      • connected to
    • Widget's sliderChanged(int) slot, which converts the int to a meaningful message and...
    • Emits a signal statusChanged(QString)
      • connected to
    • Status bar showMessage(QString) slot, which shows the message.

    Another:
    • Private slider's valueChanged(int) signal
      • connected to
    • Widget's sliderChanged(int) signal
      • connected to
    • Main window slot receiveASliderUpdateMessage(int), which converts the value to a message and calls the status bar showMessage(QString)


    The function that underlies a signal is written for you by the moc component of Qt. You do not need to write a signal function yourself. You need to write a simple slot in either the widget or main window and make sure the connections are made.
    Last edited by ChrisW67; 28th December 2010 at 04:22.

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

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

    Hi all!
    i feel a bit stupid after all these posts that I cannot implement something that seems so realistic !
    Well, I am stubborn enough to get back to this thread.

    From the two Chris' options I have algorithmically 100% understood the second one (signal to signal)

    • Private slider's valueChanged(int) signal
    • connected to
    • Widget's sliderChanged(int) signal
    Qt Code:
    1. connect(mySlider, SIGNAL(valueChanged(int)), this,SIGNAL(sendInfoToMainWindows(int) ));
    To copy to clipboard, switch view to plain text mode 


    • Widget's sliderChanged(int) signal
    • connected to
    • Main window slot receiveASliderUpdateMessage(int), which converts the value to a message and calls the status bar showMessage(QString)
    in MainWindow.cpp

    Qt Code:
    1. widget = new MyWidget;
    2. setCentralWidget(widget);
    3. connect( widget, SIGNAL( sendInfoToMainWindows(int) ), this, SLOT( printNumberInfoToStatusBar(int) ) );
    4.  
    5. void MainWindow::printNumberInfoToStatusBar (int value)
    6. {
    7. QString str;
    8. str.setNum(value);
    9. statusBar()->showMessage(str);
    10. }
    To copy to clipboard, switch view to plain text mode 

    well, so far so good.
    Now, trying to create the famous Widget's sliderChanged(int) signal
    (i called it sendInfoToMainWindows(int value) because i want to use it for other widgets,too)

    Qt Code:
    1. void MyWidget::sendInfoToMainWindows(int value)
    2. {
    3. emit mySlider->valueChanged(value);
    4. }
    To copy to clipboard, switch view to plain text mode 

    I get the error:
    error: ‘void QAbstractSlider::valueChanged(int)’ is protected

    That's the best point i have reached the last week!
    Last edited by fatecasino; 29th December 2010 at 21:56. Reason: reformatted to look better

  19. #18
    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)??

    You don't need to create an implementation of the sendInfoToMainWindows(int) signal. You just need to include it in the class definition as a signal and let Qt generate the code for you (moc).
    Qt Code:
    1. class MyWidget: public QWidget
    2. {
    3. Q_OBJECT
    4. ...
    5. signals:
    6. void sendInfoToMainWindows(int value);
    7. }
    To copy to clipboard, switch view to plain text mode 

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

    fatecasino (31st December 2010)

  21. #19
    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!!!I will check it out the soonest,
    until then,

    HAPPY NEW YEAR TO EVERYONE!!!!!!!!!!!!!!!!!!!!!!!!!!!

  22. #20
    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...new year brought a success!!!

    The function that underlies a signal is written for you by the moc component of Qt. You do not need to write a signal function yourself. You need to write a simple slot in either the widget or main window and make sure the connections are made.
    I couldn't imagine that it would simply work if I just write absolutely nothing in the implementation .cpp file about the function:

    Qt Code:
    1. void sendInfoToMainWindows(int value);
    To copy to clipboard, switch view to plain text mode 

    Now, last question on this topic:

    instead of connecting the integer value of the slider with the status bar of the MainWindow

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

    I want to sent a simple QString message from a subwidget to the status bar of the MainWindow( subwidget--signal-->centralWidget--signal-->MainWindow--slot-->statusBar), like

    Qt Code:
    1. "you tried 12 times to find the key"
    To copy to clipboard, switch view to plain text mode 
    (variable value "12" changes)

    The QSlider has a built in function valueChanged(int), how can I create a similar message QString SIGNAL sendMSGToMainWindows(QString) to connect to the MainWIndow?

    Qt Code:
    1. connect(mySubWidget, SIGNAL( ???sendMSGtoCentralWidget(QString)??? ), this,SIGNAL( sendMSGToMainWindows(QString) ));
    To copy to clipboard, switch view to plain text mode 
    Last edited by fatecasino; 3rd January 2011 at 21:56. Reason: updated contents

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.