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. What's the best way to use QTableWidgetItem(to don't waste space)

What's the best way to use QTableWidgetItem(to don't waste space)

Scheduled Pinned Locked Moved Solved General and Desktop
c++qtablewidgetfor loopsaving space
4 Posts 3 Posters 708 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.
  • M Offline
    M Offline
    Muhammad Mirab Br.
    wrote on 1 Jul 2020, 10:52 last edited by Muhammad Mirab Br. 7 Jan 2020, 11:02
    #1

    Hey, I want to have a table in MainWindow and I do, but the problem is that it's going to use lots of space because every time loop is running a new QTableWidgetItem is getting another space.

    void MainWindow::dispHelp()
    {
        int i, j, row, col, kk;
        QString test_str;
        row = 10;
        col = 5;
        for(i=0;i<row;i++)
        {
            for(j = 0; j<col; j++)
            {
                kk = i * (col) + j;
    
                test_str.setNum( kk,10);
                QTableWidgetItem* m_tblItem = new QTableWidgetItem;
    //What I want is to declare this "QTableWidgetItem* m_tblItem = new QTableWidgetItem;" once and use it as long as *for* is looping.
                m_tblItem->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEnabled);
                m_tblItem->setTextColor(Qt::black);
                m_tblItem->setText(test_str);
                ui->tbl_dbg->setItem(i,j, m_tblItem);
    
            }
        }
    }
    

    Any help will be appreciated. Thanks!

    J 1 Reply Last reply 1 Jul 2020, 11:03
    0
    • C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 1 Jul 2020, 11:03 last edited by
      #2

      @Muhammad-Mirab-Br said in What's the best way to use QTableWidgetItem(to don't waste space):

      once and use it as long as for is looping.

      How should this be possible? Every cell needs an own QTableWidgetItem.

      For big tables I would suggest to not use the convenience view widgets but a custom model derived from QAbstractItemModel or QAbstractTableModel

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      M 1 Reply Last reply 1 Jul 2020, 11:15
      3
      • M Muhammad Mirab Br.
        1 Jul 2020, 10:52

        Hey, I want to have a table in MainWindow and I do, but the problem is that it's going to use lots of space because every time loop is running a new QTableWidgetItem is getting another space.

        void MainWindow::dispHelp()
        {
            int i, j, row, col, kk;
            QString test_str;
            row = 10;
            col = 5;
            for(i=0;i<row;i++)
            {
                for(j = 0; j<col; j++)
                {
                    kk = i * (col) + j;
        
                    test_str.setNum( kk,10);
                    QTableWidgetItem* m_tblItem = new QTableWidgetItem;
        //What I want is to declare this "QTableWidgetItem* m_tblItem = new QTableWidgetItem;" once and use it as long as *for* is looping.
                    m_tblItem->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEnabled);
                    m_tblItem->setTextColor(Qt::black);
                    m_tblItem->setText(test_str);
                    ui->tbl_dbg->setItem(i,j, m_tblItem);
        
                }
            }
        }
        

        Any help will be appreciated. Thanks!

        J Offline
        J Offline
        jsulm
        Lifetime Qt Champion
        wrote on 1 Jul 2020, 11:03 last edited by
        #3
        This post is deleted!
        1 Reply Last reply
        0
        • C Christian Ehrlicher
          1 Jul 2020, 11:03

          @Muhammad-Mirab-Br said in What's the best way to use QTableWidgetItem(to don't waste space):

          once and use it as long as for is looping.

          How should this be possible? Every cell needs an own QTableWidgetItem.

          For big tables I would suggest to not use the convenience view widgets but a custom model derived from QAbstractItemModel or QAbstractTableModel

          M Offline
          M Offline
          Muhammad Mirab Br.
          wrote on 1 Jul 2020, 11:15 last edited by
          #4

          @Christian-Ehrlicher Alright, thanks!

          1 Reply Last reply
          0

          1/4

          1 Jul 2020, 10:52

          • Login

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