Results 1 to 11 of 11

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

  1. #1
    Join Date
    Oct 2020
    Posts
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

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

    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 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 is the picturer of before and after resize:

    Before:
    normal.PNG

    After:
    missing.PNG

    The items will also flicker while mouse cursor is dragged over them.

    This however doesn't happen when QTableWidget is not promoted to QDropTableWidget.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,721
    Thanks
    259
    Thanked 760 Times in 750 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: QTableWidget fails to display column/row content correctly after MainWindow resiz

    What else did you change / add when you added drag / drop event handlers?

    QTableWidget already has built-in support for drag and drop, so the changes you made are possibly interfering with the default behavior. It is also possible that you aren't correctly clearing out the existing QTableWidgetItems used by the table widget's internal model when you replace the contents with what was dropped.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Oct 2020
    Posts
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QTableWidget fails to display column/row content correctly after MainWindow resiz

    Here's the heeder and source code.
    Qt Code:
    1. #ifndef DROPAREA_H
    2. #define DROPAREA_H
    3.  
    4. #include <QTableWidget>
    5.  
    6. class QMimeData;
    7.  
    8. class QDropTableWidget : public QTableWidget
    9. {
    10. Q_OBJECT
    11.  
    12. public:
    13. QDropTableWidget(QWidget *parent = 0);
    14.  
    15. public slots:
    16. void clear();
    17.  
    18. signals:
    19. void changed(const QMimeData *mimeData = 0);
    20. void dropped(const QMimeData *mimeData = 0);
    21. void test(const QMimeData *mimeData);
    22. void OnDrop(QStringList list, int row);
    23.  
    24.  
    25. protected:
    26. void dragEnterEvent(QDragEnterEvent *event);
    27. void dragMoveEvent(QDragMoveEvent *event);
    28. void dragLeaveEvent(QDragLeaveEvent *event);
    29. void dropEvent(QDropEvent *event);
    30. void resizeEvent(QResizeEvent *event) override;
    31.  
    32. private:
    33. QTableWidget *tablewidget;
    34. };
    35.  
    36. #endif
    To copy to clipboard, switch view to plain text mode 

    Source:
    Qt Code:
    1. #include <QtGui>
    2. #include "qdroptablewidget.h"
    3. #include <qheaderview.h>
    4.  
    5. QDropTableWidget::QDropTableWidget(QWidget *parent) : QTableWidget(parent)
    6. {
    7. //set widget default properties:
    8. setFrameStyle(QFrame::Sunken | QFrame::StyledPanel);
    9. setEditTriggers(QAbstractItemView::NoEditTriggers);
    10. setDragDropMode(QAbstractItemView::DropOnly);
    11. setAlternatingRowColors(true);
    12. setShowGrid(true);
    13. setWordWrap(false);
    14.  
    15. QHeaderView *vh = this->verticalHeader();
    16. vh->setSectionResizeMode(QHeaderView::Fixed);
    17. vh->setDefaultSectionSize(20);
    18.  
    19. setSelectionBehavior(QAbstractItemView::SelectRows);
    20. //setUpdatesEnabled( true ) ;
    21.  
    22.  
    23.  
    24. }
    25.  
    26. void QDropTableWidget::dragEnterEvent(QDragEnterEvent *event) {
    27.  
    28. event->acceptProposedAction();
    29. emit changed(event->mimeData());
    30. }
    31.  
    32. void QDropTableWidget::dragMoveEvent(QDragMoveEvent *event) {
    33. event->acceptProposedAction();
    34. }
    35.  
    36. void QDropTableWidget::dropEvent(QDropEvent *event)
    37. {
    38.  
    39. event->acceptProposedAction();
    40. int row = this->rowAt(event->pos().y());
    41.  
    42.  
    43. if (event->mimeData()->hasUrls())
    44. {
    45. foreach (QUrl url, event->mimeData()->urls())
    46. {
    47.  
    48. printf("%s\n",url.toString().toStdString().c_str());
    49. list.push_back(url.toLocalFile());
    50.  
    51. }
    52. }
    53.  
    54. //emit dropped(event->mimeData());
    55. emit OnDrop(list, row);
    56.  
    57.  
    58. //emit (event->mimeData());
    59. }
    60.  
    61. void QDropTableWidget::dragLeaveEvent(QDragLeaveEvent *event)
    62. {
    63. event->accept();
    64. }
    65.  
    66. void QDropTableWidget::clear()
    67. {
    68. emit changed();
    69. }
    70.  
    71.  
    72.  
    73.  
    74.  
    75. void QDropTableWidget::resizeEvent(QResizeEvent *event)
    76. {
    77. QWidget::resizeEvent(event);
    78.  
    79.  
    80. viewport()->update();
    81. viewport()->repaint();
    82. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,721
    Thanks
    259
    Thanked 760 Times in 750 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: QTableWidget fails to display column/row content correctly after MainWindow resiz

    By overriding the drag/drop events for QTableWidget, you are preventing the normal behavior from taking place. You should probably be calling QTableWidget's handlers for these events in addition to your own.

    Who is listening for the OnDrop() signal, and what is happening there?
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  5. #5
    Join Date
    Oct 2020
    Posts
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QTableWidget fails to display column/row content correctly after MainWindow resiz

    It's connected to the MainWindow's DropToPlaylist();

    Qt Code:
    1. connect(ui.tablePlaylist, SIGNAL(OnDrop(QStringList, int)), this, SLOT(DropToPlayList(QStringList, int)));
    To copy to clipboard, switch view to plain text mode 

    After which the playlist is populated from the QStringList.

    But if there's a way to natively work with QTableWidget and use Drag&Drop like that, I would rather use that. But all the examples I managed to find on how to do
    it just say to make your own and inherit from QTableWidget.

  6. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,721
    Thanks
    259
    Thanked 760 Times in 750 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: QTableWidget fails to display column/row content correctly after MainWindow resiz

    But if there's a way to natively work with QTableWidget and use Drag&Drop like that, I would rather use that. But all the examples I managed to find on how to do
    it just say to make your own and inherit from QTableWidget.
    I am not sure, but I think QTableWidget's drag and drop support is mostly to move contents from one cell to another, not to import from files. It is sort of impossible for the table widget to know what could be the contents of any file dropped on it and how to map the content into cells.

    What does the DropToPlaylist method do? I still think that your problem is how you update the table after a drop.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  7. #7
    Join Date
    Oct 2020
    Posts
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QTableWidget fails to display column/row content correctly after MainWindow resiz

    It just populates the list with strings.

    This issue is not limited to D&D. It happens with all data. So if I just populate the list on showEvent() in MainWindow the same will happen. Data rows will show fine, but after resize the same will happen.

    Would you mind trying your self? The entire code for QDragDropWidget is in my previous post. So just place QTableWidget in resizable layout and promote it to QDDW. I'm interested to see of anyone else has this issue.

  8. #8
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,721
    Thanks
    259
    Thanked 760 Times in 750 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: QTableWidget fails to display column/row content correctly after MainWindow resiz

    Would you mind trying your self? The entire code for QDragDropWidget is in my previous post. So just place QTableWidget in resizable layout and promote it to QDDW. I'm interested to see of anyone else has this issue.
    No, because I am trying to get you to understand that the problem is not in your widget, it is how you are populating the widget with items after you have dropped something. Fine, you are populating a QStringList. You can't build the contents of a QTableWidget by passing in a QStringList, you have to create QTableWidgetItem instances and add each one to the table.

    I have asked you to show how you are doing that, because that is very likely where your error lies. Especially since you now say it happens both with and without D&D. The finger isn't pointing at your widget, it is pointing at how you are using the widget.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  9. #9
    Join Date
    Oct 2020
    Posts
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QTableWidget fails to display column/row content correctly after MainWindow resiz

    This is the function that populates the list:


    Qt Code:
    1. void MainWindow::DropToPlayList(QStringList list, int row) // row is the location of the row where the files are dropped
    2. {
    3. if (row < 0)
    4. row = 0;
    5.  
    6. for (int c=0;c<list.count();c++)
    7. {
    8. ui.tablePlaylist->insertRow(row+c);
    9.  
    10. // create all items for the row
    11. uint32_t duration = 0;
    12. for (int i=0;i<ui.tablePlaylist->columnCount();i++)
    13. {
    14. ui.tablePlaylist->setItem(row+c,i, new QTableWidgetItem("")); // create all column items for row
    15. }
    16.  
    17.  
    18. ui.tablePlaylist->item(row+c,COLUMN_START_TIME)->setText("00:00:00:00");
    19. ui.tablePlaylist->item(row+c,COLUMN_DURATION)->setText("duration");
    20. ui.tablePlaylist->item(row+c,COLUMN_TITLE)->setText("title");
    21. ui.tablePlaylist->item(row+c,COLUMN_LOCATION)->setText("location");
    22. ui.tablePlaylist->item(row+c,COLUMN_CATEGORY)->setText("category");
    23.  
    24. }
    25.  
    26. }
    To copy to clipboard, switch view to plain text mode 

  10. #10
    Join Date
    Oct 2020
    Posts
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QTableWidget fails to display column/row content correctly after MainWindow resiz

    Solution:

    QTableWidget::resizeEvent(event);

    instead of QWidget::resizeEvent(event);

  11. The following user says thank you to Milo47 for this useful post:

    d_stranz (27th October 2020)

  12. #11
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,721
    Thanks
    259
    Thanked 760 Times in 750 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: QTableWidget fails to display column/row content correctly after MainWindow resiz

    Solution:

    QTableWidget::resizeEvent(event);

    instead of QWidget::resizeEvent(event);
    Ah. Sometimes you don't notice the obvious things until they punch you in the nose. Thanks for posting your solution.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Replies: 15
    Last Post: 29th January 2020, 15:52
  2. Replies: 1
    Last Post: 20th November 2015, 10:12
  3. QListWidget: Resize to it's content
    By Fred in forum Qt Programming
    Replies: 4
    Last Post: 8th July 2014, 12:01
  4. Widget does not resize correctly
    By Demandred in forum Newbie
    Replies: 4
    Last Post: 16th April 2010, 13:06
  5. Replies: 4
    Last Post: 23rd September 2008, 13:23

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.