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. Connect slot from QWidget
Forum Updated to NodeBB v4.3 + New Features

Connect slot from QWidget

Scheduled Pinned Locked Moved General and Desktop
9 Posts 2 Posters 4.8k 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.
  • P Offline
    P Offline
    pixbyte
    wrote on last edited by
    #1

    Hello,
    Iam nwe in QT and hope to get some help for better understanding of QT here. In VC I worked in the past much with PostMessage to call a function on a child or embeeded dialog or custom control. IN Qt I read about actions/slots and to connect them. Here is the point where I have problems with this new kind of "messages":
    How can I connect a function that is placed on a QDialog with an action that is done inside a QWidget? As example, I use a QDialog and a QWidget that shows "configuration" pages on the QDialog, similar to the Configuration sample of the QT SDK.
    Now I want that a combobox indexchange event have to call a slot in the QDialog, but all I do is not really working.

    I use:
    @connect(serverCombo, SIGNAL(currentIndexChanged(int)),
    parent, SLOT(enableApply(int)));@

    I also try:
    @connect(serverCombo, SIGNAL(currentIndexChanged(int)),
    parent, SLOT(ConfigDialog::enableApply(int)));@

    This connect is inside the QWidget that is dyanmicly created after ConfigDialog was created. I also make enableApply as a public slot on the QDialog, but the function is never called from the combobox change.
    Any idea how to connect the combobox signal to the slot on the QDialog?

    Ingo

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

      Your description is incomplete. Does the signal and slot exist (defined with signals/ Q_SIGNAL and slots/ Q_SLOT in a header file)? When is this connect applied (maybe too late or in a wrong place)? Is the connection successful (the application will print a debug message if connection is not successful)? Are there any compilation or runtime errors?

      The first code snippet you have provided is a valid connection statement in Qt4 and 5 (the second one is not).

      (Z(:^

      1 Reply Last reply
      0
      • P Offline
        P Offline
        pixbyte
        wrote on last edited by
        #3

        I use:
        @public slots:
        void enableApply(int index);@

        in the header file.

        @currentIndexChanged(int)@ is a common combobox signal.

        There is no error with the first connect (second crash). But this signal never happens.

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

          That is unusual. Are you sure moc does not complain on runtime (I don't remember the exact message, but it should push something like "QObject connection failed")?

          You have not answered my other questions, too. I suspect you call connect() in a place that is not reached by the running program (a bit odd that you connect a slot in a parent widget).

          (Z(:^

          1 Reply Last reply
          0
          • P Offline
            P Offline
            pixbyte
            wrote on last edited by
            #5

            ok, maybe your last comment helped. I connect it now on the dialog, not in the widget, and it works. I use it this way:

            @
            pagesWidget->addWidget(new ConfigurationPage);
            //Now get the combobox handle and connect
            ConfigurationPage activeSubWindow = dynamic_cast<ConfigurationPage>(pagesWidget->widget(0));
            connect(activeSubWindow->serverCombo, SIGNAL(currentIndexChanged(int)),
            this , SLOT(enableApply(int)));@

            is this the right way? Or is it possible to make it shorter?

            Ingo

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

              I'm not sure casting is required. And in any case you can do this:
              @
              ConfigurationPage *temp = new ConfigurationPage();
              pagesWidget->addWidget(new ConfigurationPage);
              connect(temp->serverCombo, SIGNAL(currentIndexChanged(int)),
              this , SLOT(enableApply(int)));
              @

              Any way that works is correct. The question is rather whether it is a clean and right way to do ;)

              (Z(:^

              1 Reply Last reply
              0
              • P Offline
                P Offline
                pixbyte
                wrote on last edited by
                #7

                mybe you ment:

                @ ConfigurationPage *temp = new ConfigurationPage();
                pagesWidget->addWidget(temp);
                connect(temp->serverCombo, SIGNAL(currentIndexChanged(int)),
                this , SLOT(enableApply(int)));@

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

                  Yeah, that's what I've meant.

                  (Z(:^

                  1 Reply Last reply
                  0
                  • P Offline
                    P Offline
                    pixbyte
                    wrote on last edited by
                    #9

                    Many thanks!

                    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