QListWidgtem(Item) parent and child
-
Hello !
I want to link two QListWidget, but I don't know how to do with code. Here is what I did :
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.
-
Hi,
How are your data associated ?
Where do they come from ? -
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>
. -
Hi
Just to be sure, I understand.
Like this ?
-
Duplicate question of: https://forum.qt.io/topic/72974/link-two-qlistwidget
-
@VRonin
Oh and you provide a model based solution. Much more elegant :)
https://forum.qt.io/topic/72974/link-two-qlistwidget/4