Results 1 to 4 of 4

Thread: PyQt5 QSortFilterProxyModel hiding QWidget

  1. #1
    Join Date
    Nov 2020
    Posts
    2
    Qt products
    Platforms
    Unix/X11 Windows

    Default PyQt5 QSortFilterProxyModel hiding QWidget

    I have a QStandardItemModel that accomodates data. In one of the columns, I would like to add some QWidgets (clickable pictures). After adding QSortFilterProxyModel for soring/filtering purpose, however, the QSortFilterProxyModel hides my desired QWidgets.

    I've searched the Internet but could not find how to keep the QWidget and QSortFilterProxyModel simultaneously. It would be much appreciated if someone may guide me on this issue. Thanks.

    A minimal example, using QPushButton as my desired QWidget:

    Qt Code:
    1. from PyQt5.QtWidgets import *
    2. from PyQt5.QtCore import *
    3. from PyQt5.QtGui import *
    4.  
    5. class Buttons(QWidget):
    6. def __init__(self):
    7. super().__init__()
    8. layout = QHBoxLayout(self)
    9. layout.addWidget(QPushButton('btn1'))
    10. layout.addWidget(QPushButton('btn2'))
    11. self.setLayout(layout)
    12.  
    13. class MainWindow(QMainWindow):
    14. def __init__(self, *args, **kwargs):
    15. super(MainWindow, self).__init__(*args, **kwargs)
    16. tab = QTableView()
    17. if True: # This shows the buttons in a cell
    18. tab.setModel(sti)
    19. else: # This does not show the buttons
    20. proxy.setSourceModel(sti)
    21. tab.setModel(proxy)
    22. sti.appendRow([QStandardItem(str(i)) for i in range(5)])
    23. tab.setIndexWidget(sti.index(0, 0), QPushButton("hi"))
    24. sti.appendRow([])
    25. tab.setIndexWidget(sti.index(1, 2), Buttons())
    26. self.setCentralWidget(tab)
    27.  
    28. app = QApplication([])
    29. window = MainWindow()
    30. window.resize(800, 600)
    31. window.show()
    32. app.exec_()
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: PyQt5 QSortFilterProxyModel hiding QWidget

    Widgets placed in tables are not part of the item model used for the rest of the table's contents - they belong to the table view, not the model. If you can turn your images into QIcon or QPixmap instances, then you can install them in your model for the Qt::DecorationRole and the table will display them automatically, without the use of a separate QWidget. You have to be sure that your proxy model passes the request for this role to the source model.
    <=== 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
    Nov 2020
    Posts
    2
    Qt products
    Platforms
    Unix/X11 Windows

    Default Re: PyQt5 QSortFilterProxyModel hiding QWidget

    Hi, thank you for your reply. Does your suggestion of "DecorationRole" allow for multiple QPixmap instances in one cell?

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

    Default Re: PyQt5 QSortFilterProxyModel hiding QWidget

    Does your suggestion of "DecorationRole" allow for multiple QPixmap instances in one cell?
    No, and I don't know if you have much control over the placement of the pixmap within the cell. Possibly the AlignmentRole is observed for icons as well as text. You'll either have to make a separate cell for each pixmap or join the pixmaps into a single one for display. Probably easier to make separate cells, because I don't know if there is a way to know where in a cell the mouse was clicked. Without those coordinates, you can't tell which of the joined pixmaps was clicked.
    <=== 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. PyQt5 - Get the pixel color inside a QWidget
    By noob_user in forum Newbie
    Replies: 1
    Last Post: 15th September 2020, 20:25
  2. PyQt5 - Get the pixel color inside a QWidget
    By noob_user in forum Newbie
    Replies: 0
    Last Post: 15th September 2020, 05:59
  3. Replies: 6
    Last Post: 23rd April 2019, 21:17
  4. Replies: 1
    Last Post: 18th March 2018, 21:16

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.