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. Using Signals and Slots to link a Button to ComboBox Items
Forum Updated to NodeBB v4.3 + New Features

Using Signals and Slots to link a Button to ComboBox Items

Scheduled Pinned Locked Moved General and Desktop
7 Posts 4 Posters 9.9k 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
    retro_code
    wrote on last edited by
    #1

    I want to add items to a combobox when a button is clicked. I've had success clearing items from the combobox when pressing a button but I'm unable to add items back in by clicking a different button.

    What I have so far is:

    Contained within the header file:

    @
    public slots:
    void add_cut_width();
    @

    Contained within the source file:

    @
    void configurator_tool::add_cut_width()
    {
    ui->box_cut_width->addItems(QStringList() << "5 ft" << "6 ft");
    }

    configurator_tool::configurator_tool(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::configurator_tool)
    {
    ui->setupUi(this);

    QObject::connect(ui->button_generate, SIGNAL(clicked()),ui->box_cut_width, SLOT(configurator_tool::add_cut_width()));
    

    }
    @

    I must have a problem with the function add_cut_width because when I replace the configurator_tool::add_cut_width() with clear() in the slot then it works properly. I don't have any problems compiling or running the executable. It's just the button won't add the items into the combobox.

    I'm new to C++ and Qt so I've probably made a stupid mistake. Any help is appreciated.

    1 Reply Last reply
    0
    • G Offline
      G Offline
      goetz
      wrote on last edited by
      #2

      Your slot will not be connected (check the output on the console or debug window). You must call the slot on the class it is implemented, not on the combobox.

      This should work:

      @
      QObject::connect(ui->button_generate, SIGNAL(clicked()),this, SLOT(add_cut_width()));
      @

      http://www.catb.org/~esr/faqs/smart-questions.html

      1 Reply Last reply
      0
      • B Offline
        B Offline
        BlackDante
        wrote on last edited by
        #3

        in my opinion it's gonna be somethink like that:

        @

        configurator_tool::configurator_tool(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::configurator_tool)
        {
        ui->setupUi(this);

        connect(ui->button_generate, SIGNAL(clicked()),this, SLOT(add_cut_width()));
        

        }
        @

        Edit: Oh, Volker was first ;) // 3 replys in 4 minutes, nice :)

        sorry for my broken english :)

        1 Reply Last reply
        0
        • ZlatomirZ Offline
          ZlatomirZ Offline
          Zlatomir
          wrote on last edited by
          #4

          The problem is here:
          @QObject::connect(ui->button_generate, SIGNAL(clicked()),

          //qcombobox doesn't have that slot
          ui->box_cut_width,

          //in parentheses after SLOT you put only the slot name...
          SLOT(add_cut_width()));@

          And the second (receiver) object should be this, since it is the one that has the slot which you connect
          So this should work:
          @QObject::connect(ui->button_generate, SIGNAL(clicked()),
          this, SLOT(add_cut_width()));@

          LE: too late, hope at least my explanations are clearing the things a little-bit ;)

          https://forum.qt.io/category/41/romanian

          1 Reply Last reply
          0
          • R Offline
            R Offline
            retro_code
            wrote on last edited by
            #5

            Thanks for the super fast response guys. That fixed my problem. Looks like I need to investigate the setupUi function.

            1 Reply Last reply
            0
            • G Offline
              G Offline
              goetz
              wrote on last edited by
              #6

              setupUI basically sets up the UI parts and puts the glue to your own class.

              The two pointers in the connect method (1st and 3rd parameter) are always the object that define the signel resp. the slot you connect. This is the push button for the clicked signal and your own class for the slot. No ownership or parent-child relationship is examined in this case. The combo box doesn't even know about your own class.

              The "Signals & Slots":http://doc.qt.nokia.com/stable/signalsandslots.html doc has some more introductory info for you.

              http://www.catb.org/~esr/faqs/smart-questions.html

              1 Reply Last reply
              0
              • ZlatomirZ Offline
                ZlatomirZ Offline
                Zlatomir
                wrote on last edited by
                #7

                Why you need to investigate setupUi?
                You have another problem with setupUi()?

                Because setupUi has nothing to do with this problem, the recommended reading should be signals and slots "documentation":http://doc.qt.nokia.com/4.7/signalsandslots.html
                And some practice with C++ classes, members, encapsulation, this pointer...

                Basically the slot is member of the class you create (configurator_tool) not a member of the QComboBox and this pointer is as a pointer to the class object instance.
                You just access and modify the combobox form the slot, but the slot is part of the instance of configurator_tool class.

                LE: Volker i'm getting closer, same minute now ;) haha...

                https://forum.qt.io/category/41/romanian

                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