Using Signals and Slots to link a Button to ComboBox Items



  • 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.



  • 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()));
    @



  • 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 :)



  • 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 ;)



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



  • 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.



  • 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...


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.