Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. How can I add a widget in a QTableWidget header?
Forum Updated to NodeBB v4.3 + New Features

How can I add a widget in a QTableWidget header?

Scheduled Pinned Locked Moved Unsolved Qt for Python
qt for pythonpythonpyside
2 Posts 2 Posters 537 Views 1 Watching
  • 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.
  • E Offline
    E Offline
    etienne-monier
    wrote on last edited by etienne-monier
    #1

    Hi!

    I've got a QTableWidget with a lots of comboboxes inside it.

    Here is a minimal example.

    import sys
    
    from PySide6.QtCore import Qt
    from PySide6.QtWidgets import (
        QApplication,
        QComboBox,
        QTableWidget,
    )
    
    app = QApplication(sys.argv)
    
    headers = ["Country", "City", "Road"]
    countries = ["France", "England", "Spain"]
    cities = ["Paris", "London", "Madrid"]
    roads = ["Elysées", "Picadily circus", "Prado"]
    
    
    def get_combo(input_l: list[str]) -> QComboBox:
        combo = QComboBox()
        for item in input_l:
            combo.insertItem(10, item)
        combo.setCurrentText(input_l[0])
        return combo
    
    
    def fill_in_table(input_table: QTableWidget, N: int) -> None:
        for row in range(N):
            input_table.setCellWidget(row, 0, get_combo(countries))
            input_table.setCellWidget(row, 1, get_combo(cities))
            input_table.setCellWidget(row, 2, get_combo(roads))
    
    
    table = QTableWidget()
    table.setSortingEnabled(True)
    table.horizontalHeader().setSortIndicator(0, Qt.SortOrder.AscendingOrder)
    table.setColumnCount(len(headers))
    table.setRowCount(10)
    fill_in_table(table, 10)
    
    table.setHorizontalHeaderLabels(headers)
    
    table.show()
    app.exec()
    
    

    I want to add a filter combobox in a header. A kind of l menu to select "France" rows.
    I saw a QTableWidgetItem can be provided to fill-in a header, but I can't include a widget...

    Is it possible to achieve that?

    Thanks for your help!

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      I think this article is still valid.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1

      • Login

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