Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

    Unsolved QTablewidget horizontalscrollbar should only appear when text is too large

    General and Desktop
    4
    13
    1272
    Loading More Posts
    • 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.
    • ankit thakar
      ankit thakar last edited by

      Hello Team,

      I am working on qt 4.8 and using QTableWidget.
      There are 2 columns in my table, in second column text can be large sometimes.

      So I set columnwidth to 870. See below code

        ui->MytableWidget->setColumnCount(2);
        ui->MytableWidget->setHorizontalHeaderLabels(QStringList() << "Number" << "Summary");
        ui->MytableWidget->horizontalHeaderItem(0)->setTextAlignment(Qt::AlignLeft | Qt::AlignVCenter);
        ui->MytableWidget->horizontalHeaderItem(1)->setTextAlignment(Qt::AlignLeft | Qt::AlignVCenter);
        ui->MytableWidget->horizontalHeader()->setStretchLastSection(true);
        ui->MytableWidget->setColumnWidth(0,130);
        ui->MytableWidget->setColumnWidth(1,870);
      

      Now the problem is that in my table by default horizontal scroll bar is coming because of columnwidth 870.
      What I want is that it should only come when my text is too large (i.e. it is not fitting in the gui).

      How can I achieve this?

      1 Reply Last reply Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        Hi,

        Looks like you want to set the widget minimum size so that it's at least as big as the columns sizes.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        ankit thakar 1 Reply Last reply Reply Quote 0
        • ankit thakar
          ankit thakar @SGaist last edited by

          @SGaist
          Not exactly, what i want is that by default horizontal scrollbar should not appear. But if text size is too big so that it can't fit in gui then in that case. Scroll bar should appear.

          If i set my minimum widget size as per your suggestion then my string will be displayed in small fonts.

          1 Reply Last reply Reply Quote 0
          • Christian Ehrlicher
            Christian Ehrlicher Lifetime Qt Champion last edited by

            Then you should not set fixed column widths...

            Qt has to stay free or it will die.

            ankit thakar 1 Reply Last reply Reply Quote 0
            • ankit thakar
              ankit thakar @Christian Ehrlicher last edited by

              @Christian-Ehrlicher :-
              Exactly, I am totally agree with you.
              Let's suppose i haven't set columnwidth, then what should I write such that whenever text is too big horizontal scrollbar appears automatically. So that, user can see whole string.??

              JonB 1 Reply Last reply Reply Quote 0
              • JonB
                JonB @ankit thakar last edited by JonB

                @ankit-thakar
                Assuming you mean/are happy with a scrollbar around the whole QTableWidget, not just somehow on an individual column or cell, put the whole table inside a QScrollArea. I think scroll areas like a layout, you have to actually do QScollArea -> QLayout -> QTableWidget.

                ankit thakar 1 Reply Last reply Reply Quote 0
                • Christian Ehrlicher
                  Christian Ehrlicher Lifetime Qt Champion last edited by

                  @ankit-thakar said in QTablewidget horizontalscrollbar should only appear when text is too large:

                  then what should I write such that whenever text is too big horizontal scrollbar appears automatically

                  Are you looking for QHeaderView::setSectionResizeMode() ?

                  Qt has to stay free or it will die.

                  JonB 1 Reply Last reply Reply Quote 0
                  • JonB
                    JonB @Christian Ehrlicher last edited by

                    @Christian-Ehrlicher
                    As I understand that, it will not introduce scroll bars. The way the OP phrases the question "when my text is too large (i.e. it is not fitting in the gui)", I take that to mean he does want a scrollbar when the text is too wide to fit into the whole UI.

                    1 Reply Last reply Reply Quote 0
                    • ankit thakar
                      ankit thakar @JonB last edited by

                      @JonB :-
                      Ok here it is, the description with images.

                      When I run my code by default it is coming like this. In which by default horizontal scroll bar is coming. (Obviously because I set column width).
                      64916d75-f03d-4267-87a6-1c9924f613af-image.png

                      What I want is that, by default horizontal scroll bar should not come. so, for that what I did is that I removed the code set column width. But after that if the string is too big at that time, in UI whole string is not displayed. (Please see below image).
                      c7afc1d7-fe1e-4374-afa6-e885d2b5dc0e-image.png

                      In this case only I want horizontal scroll bar. How can I achieve that?

                      1 Reply Last reply Reply Quote 0
                      • Christian Ehrlicher
                        Christian Ehrlicher Lifetime Qt Champion last edited by

                        Did you read my post? Use the correct resize mode...

                        Qt has to stay free or it will die.

                        ankit thakar 1 Reply Last reply Reply Quote 0
                        • ankit thakar
                          ankit thakar @Christian Ehrlicher last edited by

                          @Christian-Ehrlicher :-
                          Yes, As I am working with Qt 4.8 QHeaderView::setSectionResizeMode() is not available instead of that I tried setResizeMode which is available in Qt 4.8 as below.

                          ui->MytableWidget->horizontalHeader()->setResizeMode(QHeaderView::ResizeToContents);
                          

                          But didn't worked.

                          ankit thakar 1 Reply Last reply Reply Quote 0
                          • ankit thakar
                            ankit thakar @ankit thakar last edited by

                            Any update on this???

                            1 Reply Last reply Reply Quote 0
                            • Christian Ehrlicher
                              Christian Ehrlicher Lifetime Qt Champion last edited by

                              @ankit-thakar said in QTablewidget horizontalscrollbar should only appear when text is too large:

                              Any update on this???

                              ResizeToContens works fine for me - if it does not work for you provide a minimal, compilable example. And upgrade to a supported version.

                              Qt has to stay free or it will die.

                              1 Reply Last reply Reply Quote 0
                              • First post
                                Last post