Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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



  • 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.


  • Lifetime Qt Champion

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

    QWidget::resizeEvent(event);

    You must call the base class.



  • That was short, precise and correct.

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

    Thank you.


  • Lifetime Qt Champion

    @Simula Then please mark the topic as solved :)


Log in to reply