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. send Qtable widget selected items to list widget upon button click
Forum Updated to NodeBB v4.3 + New Features

send Qtable widget selected items to list widget upon button click

Scheduled Pinned Locked Moved Solved General and Desktop
qtcreatorqtablewidgetpush buttonlistwidget
8 Posts 3 Posters 4.8k 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.
  • K Offline
    K Offline
    Kushan
    wrote on last edited by VRonin
    #1

    In my qt c++ application I have a Qtablewidget which contains 5 items( numbers from 1 to 5), a push button(named transfer) and a list widget.

    following is my code

    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();
    
    private slots:
       
        void on_Transfer_clicked();
    
    private:
        Ui::MainWindow *ui;
    };
    
    #endif // MAINWINDOW_H
    

    MainWindow.cpp

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        ui->tableWidget->setRowCount(5);
        ui->tableWidget->setColumnCount(1);
        for(int i=0;i<5;i++){
            ui->tableWidget->setItem(i, 0, new QTableWidgetItem((QString::number(i))));
        }
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    
    
    void MainWindow::on_Transfer_clicked()
    {
    
    }
    

    when I select an item from tablewidget and click the button I want to add it to the list widget! How can I do this?

    jsulmJ 1 Reply Last reply
    0
    • K Kushan

      In my qt c++ application I have a Qtablewidget which contains 5 items( numbers from 1 to 5), a push button(named transfer) and a list widget.

      following is my code

      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();
      
      private slots:
         
          void on_Transfer_clicked();
      
      private:
          Ui::MainWindow *ui;
      };
      
      #endif // MAINWINDOW_H
      

      MainWindow.cpp

      #include "mainwindow.h"
      #include "ui_mainwindow.h"
      
      MainWindow::MainWindow(QWidget *parent) :
          QMainWindow(parent),
          ui(new Ui::MainWindow)
      {
          ui->setupUi(this);
          ui->tableWidget->setRowCount(5);
          ui->tableWidget->setColumnCount(1);
          for(int i=0;i<5;i++){
              ui->tableWidget->setItem(i, 0, new QTableWidgetItem((QString::number(i))));
          }
      }
      
      MainWindow::~MainWindow()
      {
          delete ui;
      }
      
      
      
      void MainWindow::on_Transfer_clicked()
      {
      
      }
      

      when I select an item from tablewidget and click the button I want to add it to the list widget! How can I do this?

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

      @Kushan You can use http://doc.qt.io/qt-5/qtablewidget.html#selectedItems to get selected items from the table and then use http://doc.qt.io/qt-5/qlistwidget.html#insertItem-1 to add to the list widget...

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

      K 1 Reply Last reply
      0
      • jsulmJ jsulm

        @Kushan You can use http://doc.qt.io/qt-5/qtablewidget.html#selectedItems to get selected items from the table and then use http://doc.qt.io/qt-5/qlistwidget.html#insertItem-1 to add to the list widget...

        K Offline
        K Offline
        Kushan
        wrote on last edited by
        #3

        @jsulm Thanx !

        I did

        void MainWindow::on_Transfer_clicked()
        {
        ui->listWidget->addItem(ui->tableWidget->selectedItems());

        }

        and I get the following error!

        'QListWidget::addItem(QList<QTableWidgetItem*>)'
        ui->listWidget->addItem(ui->tableWidget->selectedItems());
        ^

        jsulmJ 1 Reply Last reply
        0
        • K Kushan

          @jsulm Thanx !

          I did

          void MainWindow::on_Transfer_clicked()
          {
          ui->listWidget->addItem(ui->tableWidget->selectedItems());

          }

          and I get the following error!

          'QListWidget::addItem(QList<QTableWidgetItem*>)'
          ui->listWidget->addItem(ui->tableWidget->selectedItems());
          ^

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

          @Kushan Well, it can't work this way. Please take time to read the links I posted. selectedItems returns QList<QTableWidgetItem *> and addItem() takes either a string or QStringList and an index.

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

          K 1 Reply Last reply
          0
          • jsulmJ jsulm

            @Kushan Well, it can't work this way. Please take time to read the links I posted. selectedItems returns QList<QTableWidgetItem *> and addItem() takes either a string or QStringList and an index.

            K Offline
            K Offline
            Kushan
            wrote on last edited by
            #5

            @jsulm yeah but htere are no examples in your links

            jsulmJ 1 Reply Last reply
            0
            • K Kushan

              @jsulm yeah but htere are no examples in your links

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

              @Kushan Why do you need examples? Call selectedItems and check whether the list it returns is empty - if it is empty you don't have anything to do. If it is not empty then iterate over the list and call http://doc.qt.io/qt-5/qtablewidgetitem.html#text on each element and put the result in a QString list then simply call insertItem and pass this list (untested):

              void MainWindow::on_Transfer_clicked()
              {
                  QList<QTableWidgetItem *> selectedItems = ui->tableWidget->selectedItems();
                  if (selectedItems.isEmpty())
                      return;
                  QStringList itemsToAdd;
                  for (auto item : selectedItems)
                      itemsToAdd.append(item->text());
                  ui->listWidget->addItem(0, itemsToAdd);
              }
              

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

              K 1 Reply Last reply
              3
              • jsulmJ jsulm

                @Kushan Why do you need examples? Call selectedItems and check whether the list it returns is empty - if it is empty you don't have anything to do. If it is not empty then iterate over the list and call http://doc.qt.io/qt-5/qtablewidgetitem.html#text on each element and put the result in a QString list then simply call insertItem and pass this list (untested):

                void MainWindow::on_Transfer_clicked()
                {
                    QList<QTableWidgetItem *> selectedItems = ui->tableWidget->selectedItems();
                    if (selectedItems.isEmpty())
                        return;
                    QStringList itemsToAdd;
                    for (auto item : selectedItems)
                        itemsToAdd.append(item->text());
                    ui->listWidget->addItem(0, itemsToAdd);
                }
                
                K Offline
                K Offline
                Kushan
                wrote on last edited by
                #7

                @jsulm Thanx but your one is erronous! I got it corrrected
                void MainWindow::on_Transfer_clicked()
                {
                QList<QTableWidgetItem *> selectedItems = ui->tableWidget->selectedItems();
                if (selectedItems.isEmpty()){
                return;
                }

                  for (int i=0;i<selectedItems.size();i++){
                
                  ui->listWidget->addItem(selectedItems[i]->text());
                

                }
                }

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

                  For future reference, this approach only works if you know in advance all the roles you will use.
                  You can use KSelectionProxyModel instead if you need a proper general implementation

                  "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

                  1 Reply Last reply
                  1

                  • Login

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