Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTablewidget horizontalscrollbar should only appear when text is too large
Forum Updated to NodeBB v4.3 + New Features

QTablewidget horizontalscrollbar should only appear when text is too large

Scheduled Pinned Locked Moved Unsolved General and Desktop
13 Posts 4 Posters 2.6k Views 2 Watching
  • 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 thakarA Offline
    ankit thakarA Offline
    ankit thakar
    wrote on last edited by
    #1

    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
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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 thakarA 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

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

        ankit thakarA Offline
        ankit thakarA Offline
        ankit thakar
        wrote on last edited by
        #3

        @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
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Then you should not set fixed column widths...

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          ankit thakarA 1 Reply Last reply
          0
          • Christian EhrlicherC Christian Ehrlicher

            Then you should not set fixed column widths...

            ankit thakarA Offline
            ankit thakarA Offline
            ankit thakar
            wrote on last edited by
            #5

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

            JonBJ 1 Reply Last reply
            0
            • ankit thakarA ankit thakar

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

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #6

              @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 thakarA 1 Reply Last reply
              0
              • Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @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 Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                JonBJ 1 Reply Last reply
                0
                • Christian EhrlicherC Christian Ehrlicher

                  @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() ?

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by
                  #8

                  @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
                  0
                  • JonBJ 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 thakarA Offline
                    ankit thakarA Offline
                    ankit thakar
                    wrote on last edited by
                    #9

                    @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
                    0
                    • Christian EhrlicherC Offline
                      Christian EhrlicherC Offline
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

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

                      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                      Visit the Qt Academy at https://academy.qt.io/catalog

                      ankit thakarA 1 Reply Last reply
                      0
                      • Christian EhrlicherC Christian Ehrlicher

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

                        ankit thakarA Offline
                        ankit thakarA Offline
                        ankit thakar
                        wrote on last edited by
                        #11

                        @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 thakarA 1 Reply Last reply
                        0
                        • ankit thakarA ankit thakar

                          @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 thakarA Offline
                          ankit thakarA Offline
                          ankit thakar
                          wrote on last edited by
                          #12

                          Any update on this???

                          1 Reply Last reply
                          0
                          • Christian EhrlicherC Offline
                            Christian EhrlicherC Offline
                            Christian Ehrlicher
                            Lifetime Qt Champion
                            wrote on last edited by
                            #13

                            @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 Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                            Visit the Qt Academy at https://academy.qt.io/catalog

                            1 Reply Last reply
                            0

                            • Login

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • Users
                            • Groups
                            • Search
                            • Get Qt Extensions
                            • Unsolved