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. I want to move up and down(by Pushbutton not drag and drop) qlistwidget which have list of Qlineedit

I want to move up and down(by Pushbutton not drag and drop) qlistwidget which have list of Qlineedit

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 3 Posters 1.1k 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.
  • A Offline
    A Offline
    anil_arise
    wrote on last edited by
    #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);
    
    }
    }
    

    }

    jsulmJ 1 Reply Last reply
    0
    • A anil_arise

      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);
      
      }
      }
      

      }

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @anil_arise And what does not work?
      I'm not sure this is actually needed

      listWidget->setItemWidget(currentItem,currentWidget);
      

      as currentItem already has the widget.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • A Offline
        A Offline
        anil_arise
        wrote on last edited by
        #3

        when i commented it then also give same result. widget are not show at current index

        1 Reply Last reply
        0
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #4

          Why are you using setItemWidget for a line edit?! isn't a normal QListWidgetItem enough?!

          "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

          A 1 Reply Last reply
          1
          • VRoninV VRonin

            Why are you using setItemWidget for a line edit?! isn't a normal QListWidgetItem enough?!

            A Offline
            A Offline
            anil_arise
            wrote on last edited by anil_arise
            #5

            @VRonin how can we insert QWidget directly in QListWidgetIem ? i have a QWidegt which have a QLabel and QLineEdit.

            1 Reply Last reply
            0
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by
              #6

              Inserting a widget is not the right solution. A QStyledItemDelegate subclass is the way to go

              "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

              A 1 Reply Last reply
              2
              • VRoninV VRonin

                Inserting a widget is not the right solution. A QStyledItemDelegate subclass is the way to go

                A Offline
                A Offline
                anil_arise
                wrote on last edited by
                #7

                @VRonin can you write sample code QStyledItemDelegate to insert widget in listwidget.
                and thank you

                VRoninV 1 Reply Last reply
                0
                • A anil_arise

                  @VRonin can you write sample code QStyledItemDelegate to insert widget in listwidget.
                  and thank you

                  VRoninV Offline
                  VRoninV Offline
                  VRonin
                  wrote on last edited by VRonin
                  #8

                  @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)

                  "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

                  A 1 Reply Last reply
                  1
                  • VRoninV VRonin

                    @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)

                    A Offline
                    A Offline
                    anil_arise
                    wrote on last edited by
                    #9

                    @VRonin ok thanks Now i need study and research on QStyledItemDelegate. I am new in Qt so thank you again for guiding.

                    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