I want to move up and down(by Pushbutton not drag and drop) qlistwidget which have list of Qlineedit
-
void ListWidgetUP::add(int icount)
{label1=new QLabel; QString str="label"+QString::number(icount); label1->setText(str); line = new QLineEdit; Hlayout =new QHBoxLayout; Hlayout->addWidget(label1); Hlayout->addWidget(line); widget= new QWidget; widget->setObjectName(str); widget->setLayout(Hlayout); item= new QListWidgetItem; item->setData(Qt::UserRole,icount); item->setSizeHint(QSize(80, 40)); ui->listWidget->insertItem(icount,item); ui->listWidget->setCurrentRow(icount); ui->listWidget->setItemWidget(item, widget); ui->listWidget->scrollToItem(item);
}
void ListWidgetUP::on_pushButtonUP_clicked()
{int currentIndex =listWidget->currentRow(); if (currentIndex!=-1) { if (currentIndex>0) { QListWidgetItem *currentItem= listWidget->item(currentIndex); QWidget * currentWidget=listWidget->itemWidget(currentItem); listWidget->takeItem(currentIndex); listWidget->insertItem(currentIndex-1,currentItem); listWidget->setItemWidget(currentItem,currentWidget); listWidget->setCurrentRow(currentIndex-1); } }
}
-
void ListWidgetUP::add(int icount)
{label1=new QLabel; QString str="label"+QString::number(icount); label1->setText(str); line = new QLineEdit; Hlayout =new QHBoxLayout; Hlayout->addWidget(label1); Hlayout->addWidget(line); widget= new QWidget; widget->setObjectName(str); widget->setLayout(Hlayout); item= new QListWidgetItem; item->setData(Qt::UserRole,icount); item->setSizeHint(QSize(80, 40)); ui->listWidget->insertItem(icount,item); ui->listWidget->setCurrentRow(icount); ui->listWidget->setItemWidget(item, widget); ui->listWidget->scrollToItem(item);
}
void ListWidgetUP::on_pushButtonUP_clicked()
{int currentIndex =listWidget->currentRow(); if (currentIndex!=-1) { if (currentIndex>0) { QListWidgetItem *currentItem= listWidget->item(currentIndex); QWidget * currentWidget=listWidget->itemWidget(currentItem); listWidget->takeItem(currentIndex); listWidget->insertItem(currentIndex-1,currentItem); listWidget->setItemWidget(currentItem,currentWidget); listWidget->setCurrentRow(currentIndex-1); } }
}
@anil_arise And what does not work?
I'm not sure this is actually neededlistWidget->setItemWidget(currentItem,currentWidget);
as currentItem already has the widget.
-
when i commented it then also give same result. widget are not show at current index
-
@VRonin how can we insert QWidget directly in QListWidgetIem ? i have a QWidegt which have a QLabel and QLineEdit.
-
Inserting a widget is not the right solution. A
QStyledItemDelegate
subclass is the way to go@VRonin can you write sample code QStyledItemDelegate to insert widget in listwidget.
and thank you -
@VRonin can you write sample code QStyledItemDelegate to insert widget in listwidget.
and thank you@anil_arise said in I want to move up and down(by Pushbutton not drag and drop) qlistwidget which have list of Qlineedit:
QStyledItemDelegate to insert widget in listwidget
Again. Inserting a widget is not the optimal solution. The ideal method is reimplementing
QStyledItemDelegate::paint
to paint what you need. If the widget is really complex and would need too much work to be painted manually then you can use this delegate but it should not be your 1st choice.The row move for QListWidget is a feature that was added recently and will be part of the Qt 5.13 release (gerrit reference)
-
@anil_arise said in I want to move up and down(by Pushbutton not drag and drop) qlistwidget which have list of Qlineedit:
QStyledItemDelegate to insert widget in listwidget
Again. Inserting a widget is not the optimal solution. The ideal method is reimplementing
QStyledItemDelegate::paint
to paint what you need. If the widget is really complex and would need too much work to be painted manually then you can use this delegate but it should not be your 1st choice.The row move for QListWidget is a feature that was added recently and will be part of the Qt 5.13 release (gerrit reference)
@VRonin ok thanks Now i need study and research on QStyledItemDelegate. I am new in Qt so thank you again for guiding.