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

  • 1 Votes
    6 Posts
    4k Views
    JeKK666J

    I never got around to solve this issue, so due to time constraints i reverted to compile natively on the Raspberry Pi.

  • 0 Votes
    9 Posts
    6k Views
    kshegunovK

    @Dong
    Hello,
    Sorry for the late reply. Yes, you've got the essence of it. Qt's containers (QList included) are pretty smart in respect to copying, they will not copy the actual data until you change it. They are implicitly shared. This is done so you can return and copy the container many times, but in actuality internally only a pointer is reassigned and reference counter updated. When you call a non-const function on that list, Qt checks the reference counter and if more than one object is attached to the data, then and only then the data is detached (copied). When you put your list in the QVariant it is stored by value (making a shallow copy), but QVariant will return a shallow copy as well (meaning the data will not be changed, only the internal pointer reassigned and reference counter incremented).

    Now, for your particular case:
    The first line of code works, because you don't modify the list, but take an element (which is a pointer) and modify the object the list is holding reference to. This doesn't cause the list data to be copied. The second line you have, doesn't work, because you're changing the list data (assigning a new value to a list's element), which causes the list data to be detached and in practice, you're operating on a completely different set of data.

    Here is a reference if you're interested in the way QList and other containers manage their data, and what implicit sharing is.

  • 0 Votes
    6 Posts
    2k Views
    U

    @GustavoCaldeira you are welcome, glad it worked. Could you please mark the thread as solved? Thanks!

  • 0 Votes
    4 Posts
    3k Views
    L

    Hello,

    Both inputs are highly appreciated. I understand better how, when and why should I choose pointers over refs. I'm still reading some stuff here and there on how to produce some good code.

    Memory management has always been a doozy matter in C++ and i just can't get this Stroustrup quote out of my head : "C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off. ".

    Thanks again for replying so quickly, I will apply everything i've learned here on my work asap.
    Best regards.

  • 0 Votes
    7 Posts
    3k Views
    jsulmJ

    @davidsu3344 said in [Solved] How to use Qt5 with X11?:

    but the connection speed is extremly slow

    What connection do you mean?

  • 0 Votes
    6 Posts
    2k Views
    A

    @mcosta Thank you so much. The example that you have provided me looks promising. I will start with that, and see how I can extend further.

  • 1 Votes
    7 Posts
    2k Views
    E

    hmm ok :( still thank you for the help