Skip to content
  • 0 Votes
    4 Posts
    758 Views
    JonBJ

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

  • 0 Votes
    13 Posts
    4k Views
    P

    Hello guys. Thanks for your answerS.
    The "error" was simple, stupid. In fact the _manageOrderedTable is an instance of a class called ManageOrderedTable which inherits from QTableWidget.
    In this class, there's a method called fillAll and I called setColumnCount() from there. Bad idea.
    When I call this method from upon the for loop, everything is fine. I don't understand why.
    I could understand if setColumnCount() were called too late, but in this case I should have a segfault, trying to call a cell that doesn't exist.
    Well, I don't really understand why but now it works...

    Tank you for your help, Patrick.

  • 0 Votes
    14 Posts
    8k Views
    oblivioncthO

    @mrjj
    (Since you are a Mod I applogize if this is breaking some kind of double post rule or an issue over becoming off topic)

    Also, perhaps I should make a new topic, but there is one other thing I am stuck on now. I currently have a QTableWidget in my UI with three columns. I want the first two to be fixed in width, and all three to not be changeable by the user. The third column's width I want to be a little picky about though. I want it so that it can dynamically change size so that if text is put into it that is too large it will expand with a scroll bar instead of adding a second line to the cell, which I have accomplished with:

    ui->imgTable->horizontalHeader()->setSectionResizeMode(2,QHeaderView::ResizeToContents);

    But the other condition is that I want it to always at least fill up the rest of the tables space so that there isn't a blank white area to the right of the column. Because if there is nothing in the cell and the above mode is applied the column width shrinks to the size of the header name "File Path" which is too small to take up the rest of the tables width. This normally can be accomplished by using:

    ui->imgTable->horizontalHeader()->setSectionResizeMode(2,QHeaderView::Stretch)

    but that cancels out the previous setting and makes it so that if the text gets too big a 2nd line in the cell is added (which I do not want) instead of the table gaining a scroll bar (which is what I want). So I figured, OK, all I need to do is leave it on "ResizeToContents" and then just set a minimum size so that it will dynamically resize the column but never become so small that it wont take up the whole table region. The problem I have is two fold.

    First, the closest command I could find to do this is:

    ui->imgTable->horizontalHeader()->setMinimumSectionSize(150)

    but the problem is there is no index argument so it seems that it will set a minimum size for all columns instead of just column 2. I cannot seem to find a solution that only affects one column.

    Second, even if I can set a minimum size, the size is always an integer so I probably cannot get the minimum size set perfectly so that there is no visible space between the last column and the table right extent. Therfore it would be optimal if I could somehow have a "ReizeToContents"/"Stretch" hybrid that made it stretch to the table extent but resize dynamically only if the text gets too big for the cell. Basically, "Stretch" by default, but becomes "ResizeToConetents" if the cell text length hit the limit of the cell size. It doesn't look like this can be done however. I can think of a way to cause this behavior but it would be ugly:

    Check the length of the cell text everytime a change is made (there is only one way for it to change so this isn't too hard) Since the size of the table is fixed I can determine how many characters it would take to go over the cell size, which would normally cause a 2nd line to be added Have the behavior default to stretch but change to ResizeToContents if the length of the text in the cell gets large enough that it would create a 2nd line.

    The above should give the exact behavior I am looking for but I would prefer to not having to resort to that as it is ugly, feels inefficient, and seems unnecessary for something that looks like it could take 1-3 lines if I could find the right functions.

    Does Qt have something to support this behavior or will I have to implement my awkward solution? I can take pictures if some of my explanations are unclear.