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. QListWidgtem(Item) parent and child
QtWS25 Last Chance

QListWidgtem(Item) parent and child

Scheduled Pinned Locked Moved Unsolved General and Desktop
qlistwidgetitemparentchild
7 Posts 4 Posters 3.5k 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.
  • M Offline
    M Offline
    Meugiwara
    wrote on 9 Nov 2016, 16:33 last edited by
    #1

    Hello !

    I want to link two QListWidget, but I don't know how to do with code. Here is what I did :

    alt text

    We can see two QListWidget. With the left QListWidget, I add (by example : "Bonjour", "Hello", "Tag") three QListWidgetItem. I want that if I click on one of three QListWidgetItem of the left QListWidget that I can add QListWidgetItem with the right QListWidget (by example, for "Bonjour" : "Tu", "Vas", "Bien"). If I don't click on one of three QListWidgetItem, I can't add QListWidgetItem with the right QListWidget. If I did for "Bonjour" : "Tu", "Vas", "Bien" and I click on "Hello" (obviously, "Hello" contains nothing), there is nothing in the right QListWidget. That's just an example of what I want to do. Below, I write my code if it's helpful :

    - secondwindow.cpp -

    #include "secondwindow.h"
    #include "ui_secondwindow.h"
    #include "thirdwindow.h"
    #include "ui_thirdwindow.h"
    
    SecondWindow::SecondWindow(QWidget *parent) :
        QWidget(parent),
        ui(new Ui::SecondWindow)
    {
        ui->setupUi(this);
        ui->button_1->setIcon(QIcon(":/Images/Images/Haut.png"));
        ui->button_2->setIcon(QIcon(":/Images/Images/Bas.png"));
        ui->button_5->setIcon(QIcon(":/Images/Images/Haut.png"));
        ui->button_6->setIcon(QIcon(":/Images/Images/Bas.png"));
    
        connect(ui->button_1, SIGNAL(clicked()), this, SLOT(UpForLeft()));
        connect(ui->button_2, SIGNAL(clicked()), this, SLOT(DownForLeft()));
        connect(ui->button_3, SIGNAL(clicked()), this, SLOT(DeleteForLeft()));
        connect(ui->button_4, SIGNAL(clicked()), this, SLOT(AddForLeft()));
        connect(ui->button_9, SIGNAL(clicked()), this, SLOT(ShowThirdWindow()));
        connect(ui->button_10, SIGNAL(clicked()), this, SLOT(close()));
        connect(ui->table_1, SIGNAL(itemDoubleClicked(QListWidgetItem *)),
        this, SLOT(EditForLeft(QListWidgetItem *)));
    }
    
    SecondWindow::~SecondWindow()
    {
        delete ui;
    }
    
    void    SecondWindow::ShowThirdWindow()
    {
        ThirdWindow *window = new ThirdWindow;
    
        window->setWindowTitle("B");
        window->setWindowIcon(QIcon(":/Images/Images/Bouclier.png"));
        window->setFixedSize(820, 440);
        window->show();
    }
    
    void SecondWindow::UpForLeft()
    {
        QListWidgetItem *item;
        int i;
    
        i = ui->table_1->currentRow();
        item = ui->table_1->takeItem(i);
        ui->table_1->insertItem(i - 1, item);
        ui->table_1->setCurrentRow(i - 1);
    }
    
    void SecondWindow::DownForLeft()
    {
        QListWidgetItem *item;
        int i;
    
        i = ui->table_1->currentRow();
        item = ui->table_1->takeItem(i);
        ui->table_1->insertItem(i + 1, item);
        ui->table_1->setCurrentRow(i + 1);
    }
    
    void SecondWindow::UpForRight()
    {
        QListWidgetItem *item;
        int i;
    
        i = ui->table_2->currentRow();
        item = ui->table_2->takeItem(i);
        ui->table_1->insertItem(i - 1, item);
        ui->table_1->setCurrentRow(i - 1);
    }
    
    void SecondWindow::DownForRight()
    {
        QListWidgetItem *item;
        int i;
    
        i = ui->table_2->currentRow();
        item = ui->table_2->takeItem(i);
        ui->table_1->insertItem(i + 1, item);
        ui->table_1->setCurrentRow(i + 1);
    }
    
    void SecondWindow::AddForLeft()
    {
        QString string;
    
        string = ui->line_1->text();
        ui->table_1->addItem(string);
        ui->line_1->clear();
    }
    
    void SecondWindow::DeleteForLeft()
    {
        QListWidgetItem *item;
        int i;
    
        i = ui->table_1->currentRow();
        item = ui->table_1->takeItem(i);
        delete item;
    }
    
    void SecondWindow::EditForLeft(QListWidgetItem *item)
    {
        item->setFlags(item->flags() | Qt::ItemIsEditable);
        item = ui->table_1->currentItem();
        ui->table_1->editItem(item);
    }
    

    - secondwindow.h -

    #ifndef SECONDWINDOW_H
    #define SECONDWINDOW_H
    
    #include <QListWidgetItem>
    #include <QWidget>
    #include <QString>
    #include <QIcon>
    #include "thirdwindow.h"
    #include "ui_thirdwindow.h"
    
    namespace Ui {
    class SecondWindow;
    }
    
    class SecondWindow : public QWidget
    {
        Q_OBJECT
    
    public:
        explicit SecondWindow(QWidget *parent = 0);
        ~SecondWindow();
    
    public slots:
        void ShowThirdWindow();
        void UpForLeft();
        void DownForLeft();
        void UpForRight();
        void DownForRight();
        void AddForLeft();
        void DeleteForLeft();
        void EditForLeft(QListWidgetItem *item);
    
    private:
        Ui::SecondWindow *ui;
        ThirdWindow *window;
    };
    
    #endif // SECONDWINDOW_H
    

    This is my code until now and I did nothing for what I said because I don't know how to do. Thank you for help.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 9 Nov 2016, 22:40 last edited by
      #2

      Hi,

      How are your data associated ?
      Where do they come from ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • M Offline
        M Offline
        Meugiwara
        wrote on 10 Nov 2016, 13:04 last edited by Meugiwara 11 Oct 2016, 13:04
        #3

        Hello !

        What do you mean by how my data are associated ? For the second question this is the user who add himself the programs and sessions respectively on the left and on the right :-)

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 10 Nov 2016, 16:47 last edited by
          #4

          You want to show on the right side different things based on the what you selected on the left side. Then at some point you have to store these data and associate them to be able to retrieve them properly.

          The data structure looks a bit like a QMap<QString, QStringList>.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1
          • M Offline
            M Offline
            mrjj
            Lifetime Qt Champion
            wrote on 10 Nov 2016, 18:32 last edited by
            #5

            Hi
            Just to be sure, I understand.
            Like this ?

            1 Reply Last reply
            3
            • V Offline
              V Offline
              VRonin
              wrote on 11 Nov 2016, 13:16 last edited by VRonin 11 Nov 2016, 13:16
              #6

              Duplicate question of: https://forum.qt.io/topic/72974/link-two-qlistwidget

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              M 1 Reply Last reply 11 Nov 2016, 15:46
              1
              • V VRonin
                11 Nov 2016, 13:16

                Duplicate question of: https://forum.qt.io/topic/72974/link-two-qlistwidget

                M Offline
                M Offline
                mrjj
                Lifetime Qt Champion
                wrote on 11 Nov 2016, 15:46 last edited by
                #7

                @VRonin
                Oh and you provide a model based solution. Much more elegant :)
                https://forum.qt.io/topic/72974/link-two-qlistwidget/4

                1 Reply Last reply
                1

                6/7

                11 Nov 2016, 13:16

                • Login

                • Login or register to search.
                6 out of 7
                • First post
                  6/7
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved