Hello to all! How to implement this task?

Here is the task: We have a ListWidget to populate the elements so QList <QString> list; list << "1" << "2"; <QString> list; list << "1" << "2"; etc. We have about 20 such elements. I want to move them to any position online.

There are 2 ways to solve the problem:

  1. Click on the 3 element and drag it to the position number one. It was 1,2,3, it became 3,1,2 something like a dragn drop in the listWidget. How to implement it?
  2. To the right of the ListWidget make two buttons that move the selected item to which we pressed up or down. And still it was 1,2,3 we click on the element 3 and click on the button "move one element up" it turns out 1,3,2 we click still we get 3,1,2.

So the question is how to make the first option and the second while preserving the position data after dragging it into QList?

  • and your examples of implementation can be seen ??? - Alex.B
  • one
    You create a class with ui, you push into the ui listWidget later in the cpp file constructor you create a QStringList list; You put the elements of the list << "1" << "2" << "3" there; insert it into the ui-> listWidget-> addItems (list) widget; Next, do dragn / drop with this line ui-> listWidget-> setDragDropMode (QAbstractItemView :: InternalMove); Everything plows, but how to maintain its position elements when dragging into QStringList itself? - Disastricks
  • you can save in the network when you click, and read from them - Alex.B

2 answers 2

The second option can be implemented in the usual exchange of lines for lifting up like this:

 void MainWindow::on_UpButton_clicked() { int currRow = ui->listWidget->currentRow(); if(currRow > 0){ QListWidgetItem tmp = *(ui->listWidget->currentItem()); int upRow = currRow - 1; *(ui->listWidget->item(currRow)) = *(ui->listWidget->item(upRow)); *(ui->listWidget->item(upRow)) = tmp; } lst.swap(currRow, upRow); // lst - наш лист } 

To descend an element in the same way, only take the index instead of the upper one and take the lower element to check that the current element is higher than the last.

  • Thank you, got the idea. And what about drag / drop? For example, the fact that we in the listWidget were swapped using dragn / drop by dragging it with ui-> listWidget like that? and write again in a QStringList. - Disastricks

I did everything, thanks to Shadasviar for the idea.

1) With drag / drop

mainwindow.h

 #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); public slots: void check(); private: QList<QString> list; Ui::MainWindow *ui; }; #endif // MAINWINDOW_H 

mainwindow.cpp

 #include "mainwindow.h" #include "ui_mainwindow.h" #include <QDebug> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); list << "test1" << "test2" << "test3"; ui->listWidget->addItems(list); ui->listWidget->setDragDropMode(QAbstractItemView::InternalMove); connect(ui->pushButton, SIGNAL(clicked(bool)), this, SLOT(check())); } MainWindow::~MainWindow() { delete ui; } void MainWindow::check() { for(int i = 0; i < list.count(); i++) { list[i] = ui->listWidget->item(i)->text(); qDebug() << list[i]; } } 

2) Via up / down buttons

 #ifndef CHOICEREPORTING_H #define CHOICEREPORTING_H #include <QDialog> #include <QListWidgetItem> namespace Ui { class ChoiceReporting; } class ChoiceReporting : public QDialog { Q_OBJECT public: explicit ChoiceReporting(QWidget *parent = 0); ~ChoiceReporting(); Ui::ChoiceReporting *ui; public slots: void upList(); void downList(); private: QList<QString> lst; }; 

mainwindow.cpp

 #include "choicereporting.h" #include "ui_choicereporting.h" #include <QDebug> ChoiceReporting::ChoiceReporting(QWidget *parent) : QDialog(parent), ui(new Ui::ChoiceReporting) { ui->setupUi(this); lst << "test1" << "test2" << "test3"; ui->listWidget->addItems(lst); connect(this->ui->pushButton_Up, SIGNAL(clicked()), this, SLOT(upList())); connect(this->ui->pushButton_Down, SIGNAL(clicked()), this, SLOT(downList())); } ChoiceReporting::~ChoiceReporting() { delete ui; } void ChoiceReporting::upList() { int currRow = ui->listWidget_Choice->currentRow(); if(currRow >0 ) { int upRow = currRow - 1; QListWidgetItem tmp = *(ui->listWidget_Choice->currentItem()); *(ui->listWidget_Choice->item(currRow)) = *(ui->listWidget_Choice->item(upRow)); *(ui->listWidget_Choice->item(upRow)) = tmp; lst.swap(upRow, currRow); ui->listWidget_Choice->setCurrentItem(ui->listWidget_Choice->item(upRow)); } } void ChoiceReporting::downList() { int currRow = ui->listWidget_Choice->currentRow(); if(currRow < lst.count() - 1) { int downRow = currRow + 1; QListWidgetItem tmp = *(ui->listWidget_Choice->currentItem()); *(ui->listWidget_Choice->item(currRow)) = *(ui->listWidget_Choice->item(downRow)); *(ui->listWidget_Choice->item(downRow)) = tmp; lst.swap(currRow, downRow); ui->listWidget_Choice->setCurrentItem(ui->listWidget_Choice->item(downRow)); } }