Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Problem with Combobox



  • Hello Guys. I'm writing my first biggest program in QT.

    My problem: In ComboBox(addItemCategory) I'm choosing the category for my new item, and then I'm giving name and attributes for this item. When I have done I click the push button (addItemButton) to apply. This works, but if I change the only category in Combobox(addItemCategory) then this same item is adding an automatic to other selected category BEFORE I push the button (addItemButton). I want to add items only after pushing the button (addItemButton).

    Here is my code:

    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        ui->addItemCategory->setInsertPolicy(QComboBox::NoInsert);
    }
    
    //ADD ITEM BUTTON
    void MainWindow::on_addItemButton_clicked()
    {
        if (ui->addItemName->text() == "")
        {
            QMessageBox::critical(this,"Niepowodzenei!", "Podaj nazwę produktu!",
                                                    QMessageBox::Ok);
        }
        else
        {
            ItemKcals[ItemID] = ui->addItemKcal->value();
            ItemProteins[ItemID] = ui->addItemProtein->value();
            ItemFats[ItemID] = ui->addItemFat->value();
            ItemCarbons[ItemID] = ui->addItemCarbon->value();
    
            //on_addItemCategory_activated(ItemCategory);
            //ItemCategory = ui->addItemCategory->currentIndex();
            ItemID++;
            qDebug() << ItemCategory;
    
            //Successful added item information
            QMessageBox::information(this,"Sukces!", "Pomyślnie dodałeś produkt!",
                                                    QMessageBox::Ok);
        }
    
    }
    //CHANGE ITEM CATEGORY COMBOBOX
    void MainWindow::on_addItemCategory_activated(int index)
    {  
    
        connect(ui->addItemButton,&QPushButton::clicked,this,&MainWindow::on_addItemCategory_activated);
    
        if (index==0 && ui->addItemButton)
        {
            ui->mealOneLeft->addItem(ui->addItemName->text());
        }
        else if (index==1 && ui->addItemButton)
        {
            ui->mealTwoLeft->addItem(ui->addItemName->text());
        }
        else if (index==2 && ui->addItemButton)
        {
            ui->mealThreeLeft->addItem(ui->addItemName->text());
        }
        else if (index==3 && ui->addItemButton)
        {
            ui->mealFourLeft->addItem(ui->addItemName->text());
        }
        else if (index==4 && ui->addItemButton)
        {
            ui->mealFiveLeft->addItem(ui->addItemName->text());
        }
    }
    

  • Lifetime Qt Champion

    @naax said in Problem with Combobox:

    connect(ui->addItemButton,&QPushButton::clicked,this,&MainWindow::on_addItemCategory_activated);

    This is done every time you call this function so you create more than one signal/slot connection. Move it to the ctor.



  • @Christian-Ehrlicher

    I dont know what "ctor" mean.

    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        ui->addItemCategory->setInsertPolicy(QComboBox::NoInsert);
        connect(ui->addItemButton,&QPushButton::clicked,this,&MainWindow::on_addItemCategory_activated);
    }
    

    so?


  • Lifetime Qt Champion

    @naax said in Problem with Combobox:

    I dont know what "ctor" mean.

    short form for constructor



  • @Christian-Ehrlicher

    I added, but still is adding item before pushbutton"add"

    Here is my video with this problem
    https://www.youtube.com/watch?v=KX92H2hu2bc&feature=youtu.be


  • Lifetime Qt Champion

    @naax said in Problem with Combobox:

    I added, but still is adding item before pushbutton"add"

    But in on_addItemCategory_activated() you add it so why do you wonder it's doing what you programmed?



  • @Christian-Ehrlicher

    I added this to on_addItemCategory first but this is the first time when I used connect slots. Now like I said I moved this to the constructor.


  • Lifetime Qt Champion

    In on_addItemCategory_activated() you call 'addItem()' for different widgets but you complain that this should not be done there, correct? If so then don't call addItem() there.



  • O my god... I'm so stupid... That why I should take a break for an hour from my code... Thanks a lot, bro...


Log in to reply