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. Ultra newbie Signal Slot question
Forum Updated to NodeBB v4.3 + New Features

Ultra newbie Signal Slot question

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 758 Views 1 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.
  • R Offline
    R Offline
    RedCube
    wrote on last edited by
    #1

    Hi,

    Done lots of RTFM, but would hugely appreciate a tiny bit of starter code which would help me get my head around signals and slots.

    I have a QString variable in my app class, and an LineEdit widget in the ui. Call these myString and myLineEdit.

    I'd like to set up a two-way link as follows: if the user edits the string in the UI, myString should be updated accordingly. Likewise, I'd like a function, e.g. SetString(QString newString) which can be called by other code - updating myString and updating the displayed text in myLineEdit.

    The first part I can handle I think, the second part presumably means I have to get 'SetString' to emit a signal which needs to be connected to a slot of myLineEdit. (?)

    Struggling somewhat to work out both syntax and best-practice - any advice most welcome, thanks in advance.

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      @
      class MyClass : public QWidget
      {
      Q_OBJECT
      // ...
      public slots:
      void setString(const QString &newString);
      QString getString() const;

      signals:
      // optional:
      myStringChanged(const QString &newString);

      protected:
      QString myString;
      }

      // Now, .cpp file:
      MyClass::MyClass(QWidget *parent) : QWidget(parent)
      {
      connect(myLineEdit, SIGNAL(textChanged(QString)), this, SLOT(setString(QString));
      }

      MyClass::setString(const QString &string)
      {
      myString = string;

      if (myLineEdit.text() != string) {
      myLineEdit.blockSignals(true);
      myLineEdit.setText(string);
      myLineEdit.blockSignals(false);
      }
      }
      @

      This should do all you wanted to be done :) This is of course only one of many ways to do the job.

      (Z(:^

      1 Reply Last reply
      0

      • Login

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