Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Passing data between signals and slots
QtWS25 Last Chance

Passing data between signals and slots

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
signals & slotsqt5
11 Posts 3 Posters 6.7k Views
  • 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.
  • Z Offline
    Z Offline
    Zola
    wrote on 14 Jan 2016, 13:11 last edited by Zola
    #1

    I have one class called myclass, which has member function counter(), and signal isIterrated()

    That member function counter() iterates integer and when iterated it emits signal isItterated. Only I don't understand is how to pass a that counter integer value through signal isIterrated() to slot setText(QString::number(that_int)

    I tried with this but it won't work

    connect(myclassobjpointer, SLOT(isIterrated(int)), ui->labelname, SLOT(setText(QString::number(int)));

    Can someone help me with passing data between signals and slots, I am confused.

    I get error:

    QObject::connect: No such signal for myclass::isItterated()

    ? 1 Reply Last reply 14 Jan 2016, 13:19
    0
    • Z Zola
      14 Jan 2016, 13:11

      I have one class called myclass, which has member function counter(), and signal isIterrated()

      That member function counter() iterates integer and when iterated it emits signal isItterated. Only I don't understand is how to pass a that counter integer value through signal isIterrated() to slot setText(QString::number(that_int)

      I tried with this but it won't work

      connect(myclassobjpointer, SLOT(isIterrated(int)), ui->labelname, SLOT(setText(QString::number(int)));

      Can someone help me with passing data between signals and slots, I am confused.

      I get error:

      QObject::connect: No such signal for myclass::isItterated()

      ? Offline
      ? Offline
      A Former User
      wrote on 14 Jan 2016, 13:19 last edited by A Former User
      #2

      @Zola Hi!

      SLOT(setText(QString::number(int))

      It doesn't work this way. You can't execute code inside SLOT(). You need to create a signal that emits a QString, say void isIteratedStr(QString s); and then connect this signal to the slot you mentioned:

      connect(myclassobjpointer, SLOT(isIteratedStr(QString)), ui->labelname, SLOT(setText(QString)));
      
      Z 1 Reply Last reply 14 Jan 2016, 13:20
      1
      • ? A Former User
        14 Jan 2016, 13:19

        @Zola Hi!

        SLOT(setText(QString::number(int))

        It doesn't work this way. You can't execute code inside SLOT(). You need to create a signal that emits a QString, say void isIteratedStr(QString s); and then connect this signal to the slot you mentioned:

        connect(myclassobjpointer, SLOT(isIteratedStr(QString)), ui->labelname, SLOT(setText(QString)));
        
        Z Offline
        Z Offline
        Zola
        wrote on 14 Jan 2016, 13:20 last edited by Zola
        #3

        @Wieland How to pass integer instead of string? QString::number()?

        ? 1 Reply Last reply 14 Jan 2016, 13:25
        0
        • Z Zola
          14 Jan 2016, 13:20

          @Wieland How to pass integer instead of string? QString::number()?

          ? Offline
          ? Offline
          A Former User
          wrote on 14 Jan 2016, 13:25 last edited by
          #4

          You have 3 options:

          1. What I described above.
          2. Create a proxy object with a slot that takes an integer, transforms it into a string and then emits this string.
          3. Create a new label class that inherits QLabel and add your own setTextFromInt(int) slot to that class.
          1 Reply Last reply
          1
          • Z Offline
            Z Offline
            Zola
            wrote on 14 Jan 2016, 13:34 last edited by
            #5

            @Wieland I am trying to connect signal from one thread to slot in second thread, it can be done this way you described?

            ? 1 Reply Last reply 14 Jan 2016, 13:34
            0
            • Z Zola
              14 Jan 2016, 13:34

              @Wieland I am trying to connect signal from one thread to slot in second thread, it can be done this way you described?

              ? Offline
              ? Offline
              A Former User
              wrote on 14 Jan 2016, 13:34 last edited by
              #6

              @Zola Yes, connections are thread-safe. No problem.

              Z 1 Reply Last reply 14 Jan 2016, 13:44
              0
              • ? A Former User
                14 Jan 2016, 13:34

                @Zola Yes, connections are thread-safe. No problem.

                Z Offline
                Z Offline
                Zola
                wrote on 14 Jan 2016, 13:44 last edited by
                #7

                @Wieland Thanks man!

                ? 1 Reply Last reply 14 Jan 2016, 13:45
                0
                • Z Zola
                  14 Jan 2016, 13:44

                  @Wieland Thanks man!

                  ? Offline
                  ? Offline
                  A Former User
                  wrote on 14 Jan 2016, 13:45 last edited by
                  #8

                  @Zola You're welcome :-)

                  1 Reply Last reply
                  0
                  • Z Offline
                    Z Offline
                    Zola
                    wrote on 15 Jan 2016, 08:35 last edited by Zola
                    #9

                    I am having problem passing QPixmap from one thread via emit, to main thread's ui->label slot setPixmap, is there a difference between passing QString and QPixmap objects?

                    I found this 'All widgets and several related classes, for example QPixmap, don't work in secondary threads. A secondary thread is commonly referred to as a "worker thread" because it is used to offload processing work from the main thread.' at Qt site

                    1 Reply Last reply
                    0
                    • J Offline
                      J Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on 15 Jan 2016, 09:28 last edited by
                      #10

                      Yes, UI related classes should only be used in the GUI thread.
                      You can pass the pixmap as binary data (QByteArray or uchar*) and then create the QPixmap in the GUI thread using loadFromData(...).

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      1
                      • ? Offline
                        ? Offline
                        A Former User
                        wrote on 15 Jan 2016, 10:12 last edited by A Former User
                        #11

                        Please keep in mind that sending large objects via signals by value might decrease the performance of your program. So avoid copying large objects unless you really need local copies. For a large object it's better to have only a single copy, protect access to its state via mutexes (et al.) and pass around references.

                        1 Reply Last reply
                        1

                        9/11

                        15 Jan 2016, 08:35

                        • Login

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