Skip to content
QtWS25 Last Chance
  • 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.

  • 0 Votes
    12 Posts
    3k Views
    mrjjM

    @Rohith

    np. sometimes its hard to understand the real issue
    when seeing just part of code but
    glad it worked :)

    you are very welcome.

  • 0 Votes
    2 Posts
    706 Views
    SGaistS

    Hi and welcome to devnet,

    How are you getting that value ?

  • 1 Votes
    2 Posts
    798 Views
    D

    I also have QTableWidgets inside tabs of a QTabWidget and have had this problem for a long time. Every time the .ui file is saved in QtCreator it sets the horizontal headers of all my tables to invisible except for the first table.

  • 0 Votes
    2 Posts
    828 Views
    raven-worxR

    @coow67
    connect to the model's rowsInserted() signal

  • QTableWidget memory free

    Solved General and Desktop
    3
    0 Votes
    3 Posts
    2k Views
    V

    @jsulm : Thanks for ur reply.....My doubt is cleared.

  • 0 Votes
    3 Posts
    2k Views
    Y

    Thanx bsomervi. Good point. I could go with an even more general signal (like itemClicked) but finding the signal that will more rightly pick only a checkbox checked/unchecked action would be optimal.

  • 0 Votes
    10 Posts
    8k Views
    S

    Hi
    Thank you. you helped so much. And if I can't fix the scrollbar problem I will ask it in forum

  • 0 Votes
    4 Posts
    2k Views
    kshegunovK

    @michelson said:
    Hello,

    I assumed i had to reimplement QItemDelegate::setModelData(...)

    I think this should suffice for your case. You can of course always create your own model as @SGaist suggested. As for your request for additional material:

    Here is an overview of the model-view framework A simple spinbox delegate example Icons example that uses delagets

    I hope this helps.
    Kind regards.

  • 0 Votes
    6 Posts
    9k Views
    SGaistS

    Using setText will avoid useless delete/allocation of items.

  • 0 Votes
    6 Posts
    4k Views
    SGaistS

    QComboBox has a addItems where you can directly use your QStringList, so no need for the loops. And again, you should not use static_cast like that, use object_cast.

    You're lucky it's not crashing because you only handle column 1 where you know you create a QComboBox. You should move these two lines under the if protection.

  • 1 Votes
    2 Posts
    1k Views
    O

    I finally solved the issue in reimplementing the dropEvent like that :
    if (event) {
    // Get QTableWidget
    QTableWidget *table = qobject_cast<QtableWidget *>(event->source());

    // Decode Mime Data
    QString format = QLatin1String("application/x-qabstractitemmodeldatalist");
    QByteArray encoded = event->mimeData()->data(format);
    QDataStream stream(&encoded, QIODevice::ReadOnly);

    // Decode format (row, column) to get row & column coordinates
    int row,column;
    stream >> row >> column;
    }

  • 0 Votes
    3 Posts
    2k Views
    dheerendraD

    When you set the item, The table takes ownership of the item. So just deleting the tableWidget should satisfy. If you want to selectively delete, what you are doing should be good enough.

  • 0 Votes
    3 Posts
    3k Views
    O

    Thank you
    Note that I succeed in first buiding my QTableWidget with each item set by item->setFlags(Qt::NoItemFlags) and then when I want to insert a wiget proceed with delete takeItem(row,column) before the setCellWidget(row, column, &widget) to insert a widget and retrieve the normal behavior

  • 0 Votes
    2 Posts
    1k Views
    SGaistS

    Hi,

    You can try implementing a QStyledDelegateItem and paint only the icon.

    Hope it helps

  • 1 Votes
    6 Posts
    4k Views
    mrjjM

    @DougyDrumz
    Well it would be nice if one could just grab and drag to size them.
    maybe in 6.5 :)

  • 0 Votes
    4 Posts
    4k Views
    F

    @Ratzz

    thank you !

  • 0 Votes
    8 Posts
    3k Views
    mrjjM

    @Lobstw
    Yes QRadialGradient is pretty awesome. :)
    btw there is also QLinearGradient
    http://doc.qt.io/qt-5.5/qgradient.html

    just in case u didnt see them

  • 0 Votes
    3 Posts
    2k Views
    M

    @Olivier-Ronat Thanks for checking, Olivier. Sorry to detain you to do that when I found it was my own fault. However, I learned some things and got a little better at debugging in Qt Creator.

    I found the cause. I had a signal attached to the selectionModel of each row. Here's how I found out (note, I'm a newbie). On my Mac, I got the crash report and clicked a button to see what it was. This showed a callstack. I was able to see that it was complaining about setting text() from on_SelRow, and then I recalled that I had a signal connected on rows. So, I changed my code like so:

    ui->myTable->selectionModel()->disconnect(); ui->myTable->model()->removeRows(0,ui->myTable->rowCount());

    I was then able to remove rows without a crash. I just need to remember to add the signal back to the table when I add rows again.

    P.S.

    You may also be wondering why I was intercepting a selected row. The reason was because then I could show a box above the table that showed the item detail in an easier to read format than having to scroll horizontally in the table to see all the columns. I was doing it like so:

    connect(ui->myTable->selectionModel(), SIGNAL(currentRowChanged(QModelIndex,QModelIndex)), this, SLOT(on_SelRow(QModelIndex,QModelIndex)));

    ...and then I had a class method like so:

    void MainWindow::on_SelRow(QModelIndex oCurrRow, QModelIndex oPrevRow) { QTableWidget *t = dynamic_cast<QTableWidget *>(QObject::sender()->parent()); int y = oCurrRow.row(); // do something with t (table) and y (row index) variables }
  • 0 Votes
    3 Posts
    13k Views
    mrjjM

    @maximo
    Hi
    It creates a new Header that draws a check box image on itself since its not possible to insert a real widget.
    It then response to mouse press to make the image work as a real check box.

    it checks with
    if (logicalIndex == 0)

    so I think it only paints this in first column.