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. Problem with Combobox

Problem with Combobox

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 2 Posters 679 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.
  • naaxN Offline
    naaxN Offline
    naax
    wrote on last edited by naax
    #1

    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());
        }
    }
    
    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

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

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      naaxN 1 Reply Last reply
      0
      • Christian EhrlicherC Christian Ehrlicher

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

        naaxN Offline
        naaxN Offline
        naax
        wrote on last edited by
        #3

        @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?

        1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @naax said in Problem with Combobox:

          I dont know what "ctor" mean.

          short form for constructor

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          naaxN 1 Reply Last reply
          1
          • Christian EhrlicherC Christian Ehrlicher

            @naax said in Problem with Combobox:

            I dont know what "ctor" mean.

            short form for constructor

            naaxN Offline
            naaxN Offline
            naax
            wrote on last edited by naax
            #5

            @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

            1 Reply Last reply
            0
            • Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @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?

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              naaxN 1 Reply Last reply
              1
              • Christian EhrlicherC Christian Ehrlicher

                @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?

                naaxN Offline
                naaxN Offline
                naax
                wrote on last edited by
                #7

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

                1 Reply Last reply
                0
                • Christian EhrlicherC Offline
                  Christian EhrlicherC Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  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.

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  naaxN 1 Reply Last reply
                  2
                  • Christian EhrlicherC Christian Ehrlicher

                    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.

                    naaxN Offline
                    naaxN Offline
                    naax
                    wrote on last edited by
                    #9

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

                    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