Results 1 to 17 of 17

Thread: Passing message to MainWindow status bar - using connect

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Aug 2020
    Posts
    28
    Qt products
    Qt5

    Default Re: Passing message to MainWindow status bar - using connect

    Allow me to take this one step ant at time.

    Do you see this message? Note that connect() does not return a bool, it returns an object of type QMetaObject::Connection. However, this class does have a bool() cast operator, which does return true if the connection was made. So you lucked out with this.

    Yes I see the message.

    "if" construct been checking for VALIDITY of content of parenthesis NOT for bool which is NOT basic C construct since conception.
    ( All these "false " "bool" etc. are crutches to make think complicated , not helpful...)
    The "connect" returns "handle" (YUCK ) and Qt implementation of language is doing exactly what it suppose to do - checks for VALIDITY of "handle".


    Changing the connect to EXACTLY match the SIGNAL / SLOT "signature" did not change the results - no message.
    Adding some more scaffolding I actually get helpful errors

    Qt Code:
    1. if(
    2. (bool)
    3. connect(
    4. deviceIquiry,SIGNAL(sendStatus(const QString &)),
    5. this,SLOT(displayStatusMessage(QString &))
    6. )
    7. )
    8. std::cout <<"SUCCESS SIGNALS / SLOTS CONNECTED "<< std::endl;
    9. else
    10. {
    11. std::cout <<"FAILED CONNECTION " <<std::endl;
    12. exit(-1);
    13. }
    To copy to clipboard, switch view to plain text mode 


    function BT_FormDeviceInquiry
    file ../CAT_BT/bt_formdeviceinquiry.cpp
    line # 143
    SUCCESS SIGNALS / SLOTS CONNECTED
    FAILED CONNECTION
    BT_FormDeviceInquiry *deviceIquiry : No such device or address
    BT_FormDeviceInquiry *deviceIquiry : Resource temporarily unavailable
    TEST: Resource temporarily unavailable
    QObject::connect: No such slot MainWindow::displayStatusMessage(QString &) in ../CAT_BT/mainwindow.cpp:62
    QObject::connect: (sender name: 'DeviceDiscovery')
    QObject::connect: (receiver name: 'MainWindow')
    /media/z/DEV_COPY_LABEL/Qt/QT/qtconnectivity/examples/bluetooth/build-CAT_BT-Desktop-Debug/btscanner exited with code 255

    I'll check the other stuff soon.
    Again, appreciate your support.

  2. #2
    Join Date
    Aug 2020
    Posts
    28
    Qt products
    Qt5

    Default Re: Passing message to MainWindow status bar - using connect

    Checking pointer ??

    When I add

    ui->statusbar->showMessage(message,5000);

    as part of plain "push button" function - it outputs message to status bar.

    Is that a good enough "pointer" test ?


    My "connect" never gets to execute displayStatusMessage function.


    Qt Code:
    1. void MainWindow::displayStatusMessage( const QString & message )
    2. {
    3. #ifdef TRACE
    4. std::cout <<"TRACE \nFile "<< __FILE__ << "\nFunction " <<__FUNCTION__<<" \n@line "<< __LINE__<< std::endl;
    5. #endif
    6. /*
    7.   QStatusBar * pStatusBar = statusBar();
    8.   pStatusBar->showMessage( message, 5000 ); // A 5 second timeout
    9.   */
    10. ui->statusbar->showMessage(message,5000);
    11. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,321
    Thanks
    316
    Thanked 871 Times in 858 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Passing message to MainWindow status bar - using connect

    "if" construct been checking for VALIDITY of content of parenthesis NOT for bool which is NOT basic C construct since conception.
    No, an "if" clause evaluates the expression inside it and attempts to convert it to something that can be interpreted as "true" or "false". It doesn't check "validity", whatever you think that is. To keep old code from breaking, if the expression can be converted to an integer zero (false) or non-zero (true), this will compile - so that includes numeric expressions, pointers (0 / nullptr vs. non-null), and other such things. It does not include instances of classes. As I previously said, QObject::connect() returns an instance of the class QMetaObject::Connection. It does not return a "handle" in the Windows "HANDLE" sense, it returns a class instance. A Windows HANDLE -can- be treated as a numeric value, so it can be evaluated in an if() clause. QMetaObject::Connection is not a "HANDLE", it is a C++ class. If QMetaObject::Connection did not have a QMetaObject::Connection::operator bool() cast operator, your statement would not compile, even if you try to cast it to (bool). You would get an "incompatible types" or "type conversion" error, whatever error message your compiler puts out in those cases.

    SUCCESS SIGNALS / SLOTS CONNECTED
    FAILED CONNECTION
    If your source code really is what you posted above, this combination is impossible. Either control goes through the TRUE branch or it goes through the FALSE branch, not both. The rest of the diagnostic messages you quoted doesn't make any sense either unless your real code is different from what you posted.

    Do you have two variables named "deviceIquiry"? Maybe one of them is a member variable of MainWindow, and that one is being "shadowed" by the local variable with the same name that is defined in the MainWindow constructor?

    Again, appreciate your support.
    You know, I am trying to help you out here, but you seem to be intent on just going your own way and not listening to what I and others are telling you. Making a custom signal-slot pair and connection is not hard, especially when all you want to do is to pass a string from instances of one class to another.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  4. #4
    Join Date
    Aug 2020
    Posts
    28
    Qt products
    Qt5

    Default Re: Passing message to MainWindow status bar - using connect

    Do you have two variables named "deviceIquiry"? Maybe one of them is a member variable of MainWindow, and that one is being "shadowed" by the local variable with the same name that is defined in the MainWindow constructor?

    I did search for it in the entire project - I have only ONE definition.


    Making a custom signal-slot pair and connection is not hard, especially when all you want to do is to pass a string from instances of one class to another.

    Sure . piece of cake.

  5. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,321
    Thanks
    316
    Thanked 871 Times in 858 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Passing message to MainWindow status bar - using connect

    Sure . piece of cake.
    Yes, it is, as I said earlier:

    1 - define a signal in your widget with a suitable name (sendStatus maybe) and a QString argument: void sendStatus( const QString & message )
    2 - define a slot in your MainWindow class with the same signature (void displayStatusMessage( const QString & message ))
    3 - implement code in this slot to display the string argument (message) in the status bar
    4 - when you create the widget for the tab, connect the widget's signal to the main window's slot.
    5 - when your widget wants to display a status message, call "emit sendStatus( "Here is a message" );" Since that signal is now connected to the main window's slot, the slot will take care of displaying it
    Five easy steps, and I even posted code that does it.

    Here's more. Try it. Make a new project, one that does not contain anything to do with btscanner. Implement a MainWindow class with the slot, implement a class derived from QWidget with the signal, add a pushbutton to it, and make it the central widget of the QMainWindow. When you create the widget, connect the widget signal to the MainWindow slot. Add a slot to the widget to handle the clicked() signal from the button and every time you click the button, emit the signal, exactly like this:

    Qt Code:
    1. // SignalWidget.h - no cpp file needed
    2.  
    3. #include <QWidget>
    4. #include <QVBoxLayout>
    5. #include <QPushButton>
    6.  
    7. class SignalWidget : public QWidget
    8. {
    9. Q_OBJECT
    10.  
    11. public:
    12. SignalWidget( QWidget * parent = nullptr ) : QWidget( parent )
    13. {
    14. QVBoxLayout * pLayout = new QVBoxLayout( this );
    15. QPushButton * pButton = new QPushButton( "Push Me!", this );
    16. pLayout->addWidget( pButton );
    17. setLayout( pLayout );
    18.  
    19. connect( pButton, SIGNAL( clicked() ), this, SLOT( buttonClicked() ) );
    20. }
    21.  
    22. signals:
    23. void sendStatus( const QString & message );
    24.  
    25. protected slots:
    26. void buttonClicked()
    27. {
    28. static int numClicks = 0;
    29. QString message = QString( "Button clicked %1 times" ),arg( ++numClicks );
    30. emit sendStatus( message );
    31. }
    32. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. // MainWindow.h -- no cpp file needed
    2.  
    3. #include <QMainWindow>
    4. #include <QStatusBar>
    5. #include "SignalWidget.h"
    6.  
    7. class MainWindow : public QMainWindow
    8. {
    9. Q_OBJECT
    10.  
    11. public:
    12. MainWindow( QWidget * parent = nullptr ) : QMainWindow( parent )
    13. {
    14. SignalWidget * pWidget = new SignalWidget( this );
    15. connect( pWidget, SIGNAL( sendStatus( const QString & ) ), this, SLOT( displayStatusMessage( const QString & ) ) );
    16.  
    17. setCentralWidget( pWidget );
    18. }
    19.  
    20. protected slots:
    21. void displayStatusMessage( const QString & message )
    22. {
    23. QStatusBar * pBar = statusBar();
    24. pBar->showMessage( message, 5000 );
    25. }
    26. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. // main.cpp
    2.  
    3. #include <QApplication>
    4. #include "MainWindow.h"
    5.  
    6. int main( int argc, char * argv[] )
    7. {
    8. QApplication a( argc, argv );
    9. MainWindow mw;
    10. mw.show();
    11. return a.exec()
    12. }
    To copy to clipboard, switch view to plain text mode 

    I don't guarantee that there are no typos. But it will work.

    To keep it simple, this example has no ui files (all the UI is done in code) and no cpp files except for main.cpp. You do have to make sure that MOC runs on the .h files, otherwise there will be no boilerplate code to implement the signals and slots.
    Last edited by d_stranz; 1st September 2020 at 23:36.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  6. #6
    Join Date
    Aug 2020
    Posts
    28
    Qt products
    Qt5

    Default Re: Passing message to MainWindow status bar - using connect

    I did managed to clean-up my messy code and actually got it working the way I wanted.
    I believe my problem was trying to control the btscanner from within the MainWindow.
    Currently I have both MainWindow and scanner forms on desktop and it works as expected.

    I am trying to understand QDockWidget and see if I can get btscanner to be part of the MainWindow.
    As soon as I can manage that I'll be happy camper.

  7. #7
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,321
    Thanks
    316
    Thanked 871 Times in 858 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Passing message to MainWindow status bar - using connect

    I am trying to understand QDockWidget and see if I can get btscanner to be part of the MainWindow.
    That might work. It depends on how you want your UI to operate. You could also check out QMdiArea and QMdiSubWindow as an alternative to docking widgets. If you add a QMdiArea as the central widget for QMainWindow, you can have multiple floating sub windows that can be tiled, overlapped, maximized, or minimized.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  8. #8
    Join Date
    Aug 2020
    Posts
    28
    Qt products
    Qt5

    Default Re: Passing message to MainWindow status bar - using connect

    My original idea was to use tabs in MainWindow.
    I do not like to have too cluttered application - with everything visible.
    I like to have "tab" for each functionality

    "scan for bt devices " - which btscanner does
    then I like to have info about devices paired - not really necessary
    then the actual communication via bluettoth - as a main tab


    QMdiArea and QMdiSubWindow would keep too much "stuff" visible, but it would be fun (?) to try.

Similar Threads

  1. Passing data between MainWindow and my class
    By r2com in forum Qt Programming
    Replies: 10
    Last Post: 27th July 2016, 16:04
  2. Showing long message in status bar
    By riarioriu3 in forum Qt Programming
    Replies: 1
    Last Post: 11th July 2012, 11:26
  3. Replies: 6
    Last Post: 10th February 2011, 11:10
  4. Status bar message tend to disappear
    By maverick_pol in forum Qt Programming
    Replies: 4
    Last Post: 16th July 2010, 00:15
  5. Passing Pixmaps between MainWindow and Dialog
    By ramstormrage in forum Qt Programming
    Replies: 28
    Last Post: 20th April 2008, 13: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
  •  
Qt is a trademark of The Qt Company.