Minimum size for QTableWidget collums/multiple resize modes?



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



Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.