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. QTableWidget fails to display column/row content correctly after MainWindow resize
Forum Updated to NodeBB v4.3 + New Features

QTableWidget fails to display column/row content correctly after MainWindow resize

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 170 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.
  • S Offline
    S Offline
    Simula
    wrote on last edited by
    #1

    OS: Win10 64
    Qt: 5.14.2
    IDE: VS2019
    Hi,

    I needed to implement the QTableWidget where I can drag and drop files. So made my own QDropTableWidget that inherits from QTableWidget where I then implemented dragEnterEvent(),
    dragLeaveEvent(), and dropEvent().

    I then placed a QTableWidget inside the layout with QDesigner and promoted it to QDropTableWidget and now I'm able to drop files inside the widget and populate the table this way.

    However now I have a weird issue where the table stops displaying some rows or columns after I resize the MainWindow (and horizontal layout containing the table).
    In only resets after I manually resize columns on the table. I tried to update,repain refresh table->viewPort() but the issue remains.

    Here are the pictures of before and after resize:

    https://imgur.com/a/Hqk7w4C

    Header
    ```
    #ifndef DROPAREA_H
    #define DROPAREA_H

        #include <QTableWidget>
     
        class QMimeData;
     
        class QDropTableWidget : public QTableWidget 
        {
        	Q_OBJECT
     
         	public:
         		QDropTableWidget(QWidget *parent = 0);
     
        	public slots:
        		void clear();
     
        	signals:
        		void changed(const QMimeData *mimeData = 0);
        		void dropped(const QMimeData *mimeData = 0);
                void test(const QMimeData *mimeData);
                void OnDrop(QStringList list, int row);
     
     
        	protected:
        		void dragEnterEvent(QDragEnterEvent *event);
        		void dragMoveEvent(QDragMoveEvent *event);
        		void dragLeaveEvent(QDragLeaveEvent *event);
        		void dropEvent(QDropEvent *event);
                void resizeEvent(QResizeEvent *event) override;
     
        	private:
        		QTableWidget *tablewidget;
        };
     
        #endif
    

    source

    #include <QtGui>
        #include "qdroptablewidget.h"
        #include <qheaderview.h>
     
        QDropTableWidget::QDropTableWidget(QWidget *parent) : QTableWidget(parent) 
        {
        	//set widget default properties:
        	setFrameStyle(QFrame::Sunken | QFrame::StyledPanel);
        	setEditTriggers(QAbstractItemView::NoEditTriggers);
        	setDragDropMode(QAbstractItemView::DropOnly);
        	setAlternatingRowColors(true);
        	setShowGrid(true);
        	setWordWrap(false);
     
            QHeaderView *vh = this->verticalHeader();
            vh->setSectionResizeMode(QHeaderView::Fixed);
            vh->setDefaultSectionSize(20);
     
            setSelectionBehavior(QAbstractItemView::SelectRows);
            //setUpdatesEnabled( true ) ;
     
     
     
    }
     
        void QDropTableWidget::dragEnterEvent(QDragEnterEvent *event) {
     
        	event->acceptProposedAction();
        	emit changed(event->mimeData());
        }
     
        void QDropTableWidget::dragMoveEvent(QDragMoveEvent *event) {
        	event->acceptProposedAction();
        }
     
        void QDropTableWidget::dropEvent(QDropEvent *event) 
        {
     
         	event->acceptProposedAction();
            int row = this->rowAt(event->pos().y());
     
            QStringList list;
     
              if (event->mimeData()->hasUrls())
                 {
                    foreach (QUrl url, event->mimeData()->urls()) 
                    {
     
                     printf("%s\n",url.toString().toStdString().c_str());
                     list.push_back(url.toLocalFile());
     
                   }              
                 }
     
         	//emit dropped(event->mimeData());
         	emit OnDrop(list, row);
     
     
            //emit (event->mimeData());
        }
     
        void QDropTableWidget::dragLeaveEvent(QDragLeaveEvent *event) 
        {
        	event->accept();
        }
     
        void QDropTableWidget::clear() 
        {
        	emit changed();
        }
     
     
     
     
     
    void QDropTableWidget::resizeEvent(QResizeEvent *event)
    {
        QWidget::resizeEvent(event);
     
     
       viewport()->update();
       viewport()->repaint();
    }
    

    On drop event

    void MainWindow::DropToPlayList(QStringList list, int row)  // row is the location of the row where the files are dropped
    {
     if (row < 0)
         row = 0;
     
             for (int c=0;c<list.count();c++)
                 {
                     ui.tablePlaylist->insertRow(row+c);
     
                     // create all items for the row
                     uint32_t duration = 0;
                     for (int i=0;i<ui.tablePlaylist->columnCount();i++)
                         {
                          ui.tablePlaylist->setItem(row+c,i, new QTableWidgetItem("")); // create all column items for row
                         }
     
     
                      ui.tablePlaylist->item(row+c,COLUMN_START_TIME)->setText("00:00:00:00");
                      ui.tablePlaylist->item(row+c,COLUMN_DURATION)->setText("duration");
                      ui.tablePlaylist->item(row+c,COLUMN_TITLE)->setText("title");
                      ui.tablePlaylist->item(row+c,COLUMN_LOCATION)->setText("location");
                      ui.tablePlaylist->item(row+c,COLUMN_CATEGORY)->setText("category");
     
                }
     
    }
    

    This only happens on promoted table widget. If I use ordinary QTableWidget resize works fine but I can't drag and drop files on to it.

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Simula said in QTableWidget fails to display column/row content correctly after MainWindow resize:

      QWidget::resizeEvent(event);

      You must call the base class.

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

      1 Reply Last reply
      1
      • S Offline
        S Offline
        Simula
        wrote on last edited by
        #3

        That was short, precise and correct.

        This has been bugging me for a month. I can't believe I missed it.

        Thank you.

        Christian EhrlicherC 1 Reply Last reply
        0
        • S Simula

          That was short, precise and correct.

          This has been bugging me for a month. I can't believe I missed it.

          Thank you.

          Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Simula Then please mark the topic as solved :)

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

          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