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. Connecting ComboBox 's each item with multiple images

Connecting ComboBox 's each item with multiple images

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 3.6k 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.
  • B Offline
    B Offline
    bc913
    wrote on last edited by
    #1

    Hi everyone,

    I have a groupbox and I have a combobox in it. I would like to connect each combobox item with different images (jpegs). When the user wants to select any combobox item, the image in the Graphicsview widget would be refreshed.

    And, also as the image refreshed, a specific QlineEdit and the QLabel group would be changed together. It means, I would like to connect each image with a group of Qlineedit and QLabel.

    Is it possible to do that? What kind of functions or classes should be used?

    Thanks in advance

    1 Reply Last reply
    0
    • raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      [quote author="bc913" date="1385967861"]
      Is it possible to do that? What kind of functions or classes should be used?
      [/quote]
      sure thats possible ;)
      Actually you already mentioned all classes needed.

      Setup your QComboBox and connect a slot to it's currentIndexChanged() signal. In this slot you can then set the contents of your QLineEdit, QLabel and QGraphicsView.

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

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

        Hi,

        Yes, I connect currentIndexChanged with my custom slot and when I change the index the picture is updated.

        Also, when the index is set to Index2, I want a group of lineedit and label to appears on the left side of a grid layout.

        To do this, first I define QWidget pointer in the mainwindow class and try to create new multiple lineedit and label pointers inside this widget within this slot.

        But, the problem is now, in the slot, I can't define pointer array and also even if I define single label and lineedit, when the index changed they show up in a weird position.

        I'm working with Qt creator ide and I only create Qgraphicsview and combobox via using the IDE.

        Here is the code. What are the mistakes with that code? Also, would you comment about my modeling approach?

        Thanks in advance,

        @//mainwindow.h
        #ifndef MAINWINDOW_H
        #define MAINWINDOW_H

        #include <QMainWindow>
        #include <QWidget>
        #include <QtCore>
        #include <QtGui>

        namespace Ui {
        class MainWindow;
        }

        class MainWindow : public QMainWindow
        {
        Q_OBJECT

        public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();

        private slots:
        void updateGraphView();

        private:
        Ui::MainWindow *ui;
        QGraphicsScene *sceneG5;
        QWidget *widget;
        QGridLayout *glayout;

        };

        #endif // MAINWINDOW_H@

        @//mainwindow.cpp
        #include "mainwindow.h"
        #include "ui_mainwindow.h"

        MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
        {
        ui->setupUi(this);
        QString *names = new QString[4];
        names[0] = "Index1";
        names[1] = "Index2";
        names[2] = "Index3";
        names[3] = "Index4";

            for(int i = 0; i<4;i++) //Combobox in (itemlerine )indexlerine isim atamasi
            {
                ui -> comboBox ->addItem(names[i]);
            }
        
            connect(ui -> comboBox, SIGNAL(currentIndexChanged(QString)), this, SLOT(updateGraphView()));
        

        }

        MainWindow::~MainWindow()
        {
        delete ui;
        }

        void MainWindow :: updateGraphView()
        { // Image setup
        if (ui ->comboBox -> currentText() == "Index2") {
        sceneG5 = new QGraphicsScene();
        ui -> graphicsView -> setScene(sceneG5);
        QGraphicsPixmapItem *image1 = new QGraphicsPixmapItem(QPixmap("D:\bc913\capture\Capture122.jpg"));
        sceneG5 ->addItem(image1);
        ui -> graphicsView ->show();
        // LAbel and Lineedit setup
        widget = new QWidget(this);
        glayout = new QGridLayout(this);
        for (int i = 0; i < 5; ++i){
        QLabel *RveLabs[i] = new QLabel(QString(tr("Random")),widget);
        QLineEdit *RveInputs[i] = new QLineEdit(widget);}

            QFormLayout *flayout = new QFormLayout(widget);
        
            for (int i = 0; i < 5; ++i){
                flayout -> addRow(Rve1Labs[i],RveInputs[i]);} 
        
            widget -> setLayout(flayout);
            glayout -> addWidget(ui -> comboBox, 0, 0, 1, 2);
            glayout -> addWidget(ui -> graphicsView, 0, 2, 8, 8);
            glayout -> addWidget(widget, 1, 0,9 ,2 );
            this -> setLayout(glayout);
        }
        else
            ui -> graphicsView -> hide();
        

        }@

        1 Reply Last reply
        0
        • raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

          your updateGraphView() method seems very weird to me.
          You create widgets but never remove/delete them (at least in the code you've posted).

          Also from the Qt docs of QWidget::setLayout():
          [quote]
          If there already is a layout manager installed on this widget, QWidget won't let you install another. You must first delete the existing layout manager (returned by layout()) before you can call setLayout() with the new layout.
          [/quote]

          So the clean way would be:
          Create your widgets and layout in the constructor and add and display them when your index gets selected.
          Or create them "on-the-fly" in the slot when your index gets selected.

          @
          void MainWindow :: updateGraphView()
          { // Image setup
          if (ui ->comboBox -> currentText() == "Index2")
          {
          //set your image
          ui -> graphicsView ->show();

             //Label and Lineedit setup
             QFormLayout *flayout = new QFormLayout;
             QWidget* widget = new QWidget;
                  widget->setLayout(flayout);
             QGridLayout* glayout = new QGridLayout;
              for (int i = 0; i < 5; ++i)
             {
                  QLabel *label = new QLabel(QString(tr("Random")));
                  QLineEdit *lineEdit = new QLineEdit;
                  flayout -> addRow(label,lineEdit);
              }
          
              glayout -> addWidget(ui -> comboBox, 0, 0, 1, 2);
              glayout -> addWidget(ui -> graphicsView, 0, 2, 8, 8);
              glayout -> addWidget(widget, 1, 0,9 ,2 );
          
              //remove everything from the layout first
              QLayoutItem *item;
              while((item = this->layout()->takeAt(0))) 
                     delete item;   //only deletes layout item not the widget
          
              this -> setLayout(glayout);
          }
          else
              ui -> graphicsView -> hide();
          

          }
          @

          Also you do not need to create a new QGraphicsScene everytime... unless you have a good reason to of course :)

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          0
          • B Offline
            B Offline
            bc913
            wrote on last edited by
            #5

            Thanks but I couldn't fix the issue. I think miss out a fundamental thing. But when i try to select index2, the app crashes. It gives this warning:
            "QWidget::setLayout: Attempting to set QLayout "" on QWidget "", which already has a layout"

            How can I manage changeable (update - able) GUI?

            @//main.cpp

            #include <QApplication>

            #include "img_proc.h"

            int main(int argc, char *argv[])
            {
            QApplication app(argc, argv);
            Img_proc Bc;

            Bc.showMaximized();
            return app.exec();
            

            }
            @

            @//img_proc.h
            #ifndef IMG_PROC_H
            #define IMG_PROC_H

            #include <QMainWindow>
            #include <QComboBox>
            #include <QGraphicsView>
            #include <QLineEdit>
            #include <QLabel>
            #include <QWidget>
            #include <QFormLayout>
            #include <QGridLayout>

            class Img_proc : public QMainWindow
            {
            Q_OBJECT

            public:
            Img_proc();
            private:
            enum{Numlab = 3};
            QWidget *window;
            QComboBox *combo;
            QGraphicsView *graph1;
            QGraphicsScene * scene;
            // QLabel *Rve1Labs[Numlab];
            // QLineEdit *Rve1Input[Numlab];
            QLabel *Rve2Labs[Numlab-1];
            QLineEdit *Rve2Input[Numlab-1];
            // QLabel *Rve3Labs[Numlab-2];
            // QLineEdit *Rve3Input[Numlab-2];
            // QFormLayout *flayout;
            // QGridLayout *glayout;
            // QFormLayout *flayout1;
            // QGridLayout *glayout1;
            QFormLayout *flayout2;
            QGridLayout *glayout2;
            // QFormLayout *flayout3;
            // QGridLayout *glayout3;
            private slots:
            void updateGraphView();
            };

            #endif // IMG_PROC_H
            @

            @//img_proc.cpp
            #include <QtGui>
            #include "img_proc.h"

            Img_proc::Img_proc()
            {
            window = new QWidget();
            this -> setCentralWidget(window);
            combo = new QComboBox();
            graph1 = new QGraphicsView();
            scene = new QGraphicsScene();
            graph1 -> setScene(scene);

            QGridLayout *glayout = new QGridLayout();
            //INdex 1
            QString *names = new QString[Numlab];
            names[0] = "Index1"; names[1] = "Index2";names[2] = "Index3";
            for(int i = 0; i<Numlab;i++)
            {
                combo->addItem(names[i]);
            }
            glayout -> addWidget(combo, 0, 0, 1, 2);
            glayout -> addWidget(graph1, 0, 2, 8, 8);
            window -> setLayout(glayout);
            connect(combo, SIGNAL(currentIndexChanged(QString)), this, SLOT(updateGraphView()));
            

            }

            void Img_proc:: updateGraphView()
            { // Image setup
            if (combo -> currentText() == "Index2") {
            QGraphicsPixmapItem *image1 = new QGraphicsPixmapItem(QPixmap("D:\bc913\capture\Capture122.jpg"));
            scene ->addItem(image1);
            graph1->show();

               flayout2 = new QFormLayout();
               glayout2 = new QGridLayout();
                for(int i = 0; i<Numlab;i++)
                {
                    Rve2Labs[i] = new QLabel(QString(tr("random")));
                    Rve2Input[i] = new QLineEdit();
                    flayout2 -> addRow(Rve2Labs[i],Rve2Input[i]);
                }
            
                glayout2 -> addWidget(combo, 0, 0, 1, 2);
                glayout2 -> addWidget(graph1, 0, 2, 8, 8);
                glayout2 -> addLayout(flayout2, 1, 0,9 ,2 );
            
                QLayoutItem *item;
                while((item = window->layout()->takeAt(0)))
                             delete item;
                window -> setLayout(glayout1);
            
            }
            else
                graph1 -> hide();
            

            }
            @

            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