Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Whether to use signals/slots, or a function call from MainWindow added to a project to display QString return value from function in other Widget
Forum Updated to NodeBB v4.3 + New Features

Whether to use signals/slots, or a function call from MainWindow added to a project to display QString return value from function in other Widget

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 288 Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    DavidEdwards
    wrote on 24 Oct 2024, 01:56 last edited by
    #1

    I am using the QWidget from an existing project I found at https://github.com/confidentFeng/QtAppProject/tree/master/WifiList

    In a Qt Widgets Application I have added a QLabel, and a QPushButton on the MainWindow which opens an instance of the WifiList widget from the github project.

    My goal is to display in a QLabel (or QLineEdit or similar text-displaying object) the QString return value from QString WifiList::getConectedWifi(), line 105 in WifiList.cpp.

    On another post, Access an existing class and use QDialog::accept in a connect() to pass a QString value param?, a suggestion is: "The usual way of getting, say, a string back from a dialog it invokes does not involve any signal/slot. Instead the dialog should expose a getter."

    My question are signals and slots required, or recommended, to do this, or can the WifiList::getConectedWifi() function be called from the mainwindow to display the QString value on the QLabel in the main window (as in the below mock-up):
    WifiConnected.png

    QString values from qDebug() in the output
    Screenshot 2024-10-23 183127.png

    A P 2 Replies Last reply 24 Oct 2024, 02:52
    0
    • D DavidEdwards
      24 Oct 2024, 01:56

      I am using the QWidget from an existing project I found at https://github.com/confidentFeng/QtAppProject/tree/master/WifiList

      In a Qt Widgets Application I have added a QLabel, and a QPushButton on the MainWindow which opens an instance of the WifiList widget from the github project.

      My goal is to display in a QLabel (or QLineEdit or similar text-displaying object) the QString return value from QString WifiList::getConectedWifi(), line 105 in WifiList.cpp.

      On another post, Access an existing class and use QDialog::accept in a connect() to pass a QString value param?, a suggestion is: "The usual way of getting, say, a string back from a dialog it invokes does not involve any signal/slot. Instead the dialog should expose a getter."

      My question are signals and slots required, or recommended, to do this, or can the WifiList::getConectedWifi() function be called from the mainwindow to display the QString value on the QLabel in the main window (as in the below mock-up):
      WifiConnected.png

      QString values from qDebug() in the output
      Screenshot 2024-10-23 183127.png

      A Offline
      A Offline
      Axel Spoerl
      Moderators
      wrote on 24 Oct 2024, 02:52 last edited by
      #2

      @DavidEdwards said in Whether to use signals/slots, or a function call from MainWindow added to a project to display QString return value from function in other Widget:

      are signals and slots required, or recommended, to do this or can the WifiList::getConectedWifi() function be called from the mainwindow to display the QString value on the QLabel in the main window (as in the below mock-up):

      If WifiList lives in the same thread as MainWindow. you can call the getter directly. If it lives in a different thread, you have to invoke it. That solution works in a safe pull-environment. In other words:

      • It can safely be assumed that the getter is called on a valid object
      • MainWindow controls when it fetches the string to display

      In a push-environment, where the label / line edit has to be automatically updated if the connected WiFi changes, signals and slots are the right implementation. You would have to implement a connectedWifiChanged(const QString &wifi) signal in WifiListand connect it to the label's / line edit's setText(const QString &text) slot. Besides the automatic update, the advantage is that the MainWindow doesn't have to know if WifiList is still alive. If it's destroyed, the connection is disconnected as well.

      Software Engineer
      The Qt Company, Oslo

      1 Reply Last reply
      1
      • D DavidEdwards
        24 Oct 2024, 01:56

        I am using the QWidget from an existing project I found at https://github.com/confidentFeng/QtAppProject/tree/master/WifiList

        In a Qt Widgets Application I have added a QLabel, and a QPushButton on the MainWindow which opens an instance of the WifiList widget from the github project.

        My goal is to display in a QLabel (or QLineEdit or similar text-displaying object) the QString return value from QString WifiList::getConectedWifi(), line 105 in WifiList.cpp.

        On another post, Access an existing class and use QDialog::accept in a connect() to pass a QString value param?, a suggestion is: "The usual way of getting, say, a string back from a dialog it invokes does not involve any signal/slot. Instead the dialog should expose a getter."

        My question are signals and slots required, or recommended, to do this, or can the WifiList::getConectedWifi() function be called from the mainwindow to display the QString value on the QLabel in the main window (as in the below mock-up):
        WifiConnected.png

        QString values from qDebug() in the output
        Screenshot 2024-10-23 183127.png

        P Offline
        P Offline
        Pl45m4
        wrote on 24 Oct 2024, 03:03 last edited by Pl45m4
        #3

        @DavidEdwards

        Hi and welcome to the forum!

        First thing I've noticed: your widget does not have any layout... for testing it's okay, but later you should add a proper layout strategy otherwise you will face unwanted behavior on resizing or moving your window around.

        Regarding your inital question:
        As always, the answer is "it depends"...
        Both ways are possible, but one fits a certain situation more than another.

        My question are signals and slots required, or recommended, to do this, or can the WifiList::getConectedWifi() function be called from the mainwindow to display the QString value on the QLabel in the main window (as in the below mock-up):

        No they are not required, not in this situation.

        When your dialog finishes with accepted status, you can use the function(s) to retrieve the data you want to use.

        However @JonB 's suggestion involves QDialog::exec() which is not recommended (anymore), according to its documentation.
        If you know what you are doing there's still nothing wrong with it.
        (Documentation is refering to a situation where the parent might be destroyed while the dialog's event loop is running, which shouldn't happen, when used correctly)

        The alternative, showing your dialog with QDialog::open() is asynchronous.
        Means, no own event loop, not blocking the code execution of the parent.
        Therefore you can't tell when you are safe to call your getters...
        Using Qt Signals can solve this... either by connecting the accepted signal to a slot or by using a lambda expression.


        If debugging is the process of removing software bugs, then programming must be the process of putting them in.

        ~E. W. Dijkstra

        1 Reply Last reply
        1
        • D Offline
          D Offline
          DavidEdwards
          wrote on 26 Oct 2024, 03:26 last edited by
          #4

          Hi and thank you for your warm welcome @Pl45m4 !

          Thank you @Axel-Spoerl and @Pl45m4 for your detailed insights and sharing your expertise.

          Your explanations helped me to best understand my options and the requirement for my chosen implementation.

          I intend to have push-environment. Once getting the signal / slot implemented correctly on the appropriate components, and understanding the object who knows where the signal should go should do the connections - it worked.

          @Pl45m4 and @Axel-Spoerl thank you very much for your assistance and through explanations!

          1 Reply Last reply
          0
          • D DavidEdwards has marked this topic as solved on 26 Oct 2024, 03:27

          1/4

          24 Oct 2024, 01:56

          • Login

          • Login or register to search.
          1 out of 4
          • First post
            1/4
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved