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. Remove CellWidget from QTableWidget when focus out
QtWS25 Last Chance

Remove CellWidget from QTableWidget when focus out

Scheduled Pinned Locked Moved Unsolved General and Desktop
qtablewidgetsetcellwidgetremove widget
4 Posts 2 Posters 1.0k 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
    shobhitmittal
    wrote on last edited by shobhitmittal
    #1

    I have a QTableWidget in which some cells have QComboBox as cellWidget. I want to create the comboBox only when the user clicks in the cell. For this, I have caught itemClicked signal and created the comboBox and set it as cellWidget.

    Now, I want to delete this comboBox in two scenarios - if the user selects some value from the comboBox, or the user simply focusses out (click anywhere in the dialog).

    For the first case, I have use the signal QComboBox::activated which is invoked when something is selected in the drop-down. There I delete the comboBox and it works fine.

    However, I am not sure how to proceed for the second case (delete the comboBox when the user focusses out of the drop-down without selecting anything).

    I tried capturing eventFilter for focusOut for the tablewidget as well as the comboBox, but it didn't help.

    JonBJ 1 Reply Last reply
    0
    • S shobhitmittal

      I have a QTableWidget in which some cells have QComboBox as cellWidget. I want to create the comboBox only when the user clicks in the cell. For this, I have caught itemClicked signal and created the comboBox and set it as cellWidget.

      Now, I want to delete this comboBox in two scenarios - if the user selects some value from the comboBox, or the user simply focusses out (click anywhere in the dialog).

      For the first case, I have use the signal QComboBox::activated which is invoked when something is selected in the drop-down. There I delete the comboBox and it works fine.

      However, I am not sure how to proceed for the second case (delete the comboBox when the user focusses out of the drop-down without selecting anything).

      I tried capturing eventFilter for focusOut for the tablewidget as well as the comboBox, but it didn't help.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @shobhitmittal
      Hello and welcome.

      It sounds like you are trying to re-invent the editing capabilities already built into QTableWidget/QTableView, without any need to call setCellWidget() which is "frowned upon". The inbuilt facilities are capable of showing and removing a QComboBox only when editing. Have you looked at/understood that, before you start doing your own stuff here?

      S 1 Reply Last reply
      0
      • JonBJ JonB

        @shobhitmittal
        Hello and welcome.

        It sounds like you are trying to re-invent the editing capabilities already built into QTableWidget/QTableView, without any need to call setCellWidget() which is "frowned upon". The inbuilt facilities are capable of showing and removing a QComboBox only when editing. Have you looked at/understood that, before you start doing your own stuff here?

        S Offline
        S Offline
        shobhitmittal
        wrote on last edited by
        #3

        @JonB As far as I understand, if I need to show a comboBox in a tableWidget cell, I need to use setCellWidget with correct row, column and the widget. As soon as I do so, the comboBox is visible in the cell. Though the comboBox is not pop-down, but the notch (down arrow) is visible which makes one understand that there is a comboBox box and I want to avoid it. I want to show the comboBox only when I click in that cell and not before or after that.

        As mentioned in my question, showing is straightforward. Even removing the widget is fine when something is selected from the comboBox. But, if nothing is selected, then how to remove the widget, that' my question.

        JonBJ 1 Reply Last reply
        0
        • S shobhitmittal

          @JonB As far as I understand, if I need to show a comboBox in a tableWidget cell, I need to use setCellWidget with correct row, column and the widget. As soon as I do so, the comboBox is visible in the cell. Though the comboBox is not pop-down, but the notch (down arrow) is visible which makes one understand that there is a comboBox box and I want to avoid it. I want to show the comboBox only when I click in that cell and not before or after that.

          As mentioned in my question, showing is straightforward. Even removing the widget is fine when something is selected from the comboBox. But, if nothing is selected, then how to remove the widget, that' my question.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @shobhitmittal said in Remove CellWidget from QTableWidget when focus out:

          As far as I understand, if I need to show a comboBox in a tableWidget cell, I need to use setCellWidget

          No, you do not need to do this (via setCellWidget()).

          Search the QTableWidget docs page, and that of QTableView from which it inherits, for edit. Qt has the "framework" for going in & out of edit mode on a cell without you having to to ever call setCellWidget. I believe that by doing that you will find that if the user clicks away from the combobox editor (which appears when editing starts) the framework will get rid of that comboxbox for you.

          Actually the editor facilities are shown under QAbstractItemView Class, from which QTreeView (and therefore QTableWidget too) inherits. You will use code including

          tableWidget->setEditTriggers(QAbstractItemView::AllEditTriggers);
          item->setFlags(item->flags() | Qt::ItemIsEditable);
          

          Those lines make an item editable by double-clicking (I think) on a cell.

          Ultimately you should use void QAbstractItemView::setItemDelegate(QAbstractItemDelegate *delegate). You should subclass your own QAbstractItemDelegate. Override its QWidget *QAbstractItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const. Have that return a new QComboBox for your case. Then the Qt infrastructure will be responsible for showing and destroying the combobox for you.

          Qt's Star Delegate Example provides editablity and shows what you need to do. There may be other examples. Ultimately you will be creating a QStyledItemDelegate and overriding its "editor" methods to return a combobox to use and populating that combo's items.

          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