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. turn off elide in column of qtablewidget
Forum Updated to NodeBB v4.3 + New Features

turn off elide in column of qtablewidget

Scheduled Pinned Locked Moved Solved General and Desktop
18 Posts 4 Posters 2.5k Views
  • 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.
  • S Offline
    S Offline
    sawarsi
    wrote on last edited by
    #1

    Hello,

    i have a table widget derived from QTableWidget and first i tried the following:
    setTextElideMode(Qt::ElideNone) in the constructor and this did not work. so i setup a delegate as:

    class elideNoneItemC : public QStyledItemDelegate
    {
    virtual void paint(QPainter *painter, const QStyleOptionViewItem &option,
    const QModelIndex &index) const
    {
    QStyleOptionViewItem opt = option;
    opt.textElideMode=Qt::ElideNone;
    QStyledItemDelegate::paint(painter, opt, index);
    }
    };

    elideNoneItemC *elidenone = new elideNoneItemC;

    and then setItemDelegateForColumn(1,elidenone); for the second column but the text in the second column (if sufficiently long) still displays the ellipses.

    any idea what may be wrong.

    1 Reply Last reply
    0
    • eyllanescE Offline
      eyllanescE Offline
      eyllanesc
      wrote on last edited by eyllanesc
      #2

      @sawarsi You have to override the initStyleOption method

      class StyledItemDelegate: public QStyledItemDelegate
      {
      public:
          using QStyledItemDelegate::QStyledItemDelegate;
      protected:
          void initStyleOption(QStyleOptionViewItem *option, const QModelIndex &index) const override
          {
              QStyledItemDelegate::initStyleOption(option, index);
              option->textElideMode = Qt::ElideNone;
          }
      };
      

      If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

      S JonBJ C 3 Replies Last reply
      2
      • eyllanescE eyllanesc

        @sawarsi You have to override the initStyleOption method

        class StyledItemDelegate: public QStyledItemDelegate
        {
        public:
            using QStyledItemDelegate::QStyledItemDelegate;
        protected:
            void initStyleOption(QStyleOptionViewItem *option, const QModelIndex &index) const override
            {
                QStyledItemDelegate::initStyleOption(option, index);
                option->textElideMode = Qt::ElideNone;
            }
        };
        
        S Offline
        S Offline
        sawarsi
        wrote on last edited by
        #3

        @eyllanesc thanks i tried what u suggested but i am still getting the ellipses. so i don't need to have the paint function at all?

        1 Reply Last reply
        0
        • eyllanescE eyllanesc

          @sawarsi You have to override the initStyleOption method

          class StyledItemDelegate: public QStyledItemDelegate
          {
          public:
              using QStyledItemDelegate::QStyledItemDelegate;
          protected:
              void initStyleOption(QStyleOptionViewItem *option, const QModelIndex &index) const override
              {
                  QStyledItemDelegate::initStyleOption(option, index);
                  option->textElideMode = Qt::ElideNone;
              }
          };
          
          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @eyllanesc
          How do we know which QStyleOptionViewItem things to do in initStyleOption() versus what @sawarsi tried to do in the paint() override?

          Ah, I see it says:

          When reimplementing paint in a subclass. Use the initStyleOption() to set up the option in the same way as the QStyledItemDelegate.

          but not sure I understand "in the same way as"?

          Given that paint() takes a const QStyleOptionViewItem &option, are you not supposed to alter it and pass it on as he did? Is the option intended to be read-only at this stage?

          1 Reply Last reply
          0
          • eyllanescE Offline
            eyllanescE Offline
            eyllanesc
            wrote on last edited by eyllanesc
            #5

            @JonB

            paint receives the option but then modifies it by invoking the initStyleOption. If you want to know how a class works then it is better to check the source code: https://code.qt.io/cgit/qt/qtbase.git/tree/src/widgets/itemviews/qstyleditemdelegate.cpp?h=dev#n377

            void QStyledItemDelegate::paint(QPainter *painter,
                    const QStyleOptionViewItem &option, const QModelIndex &index) const
            {
                Q_ASSERT(index.isValid());
            
                QStyleOptionViewItem opt = option;
                initStyleOption(&opt, index); // <--- update option
            
                const QWidget *widget = QStyledItemDelegatePrivate::widget(option);
                QStyle *style = widget ? widget->style() : QApplication::style();
                style->drawControl(QStyle::CE_ItemViewItem, &opt, painter, widget);
            }
            

            @sawarsi you should not modify the paint method

            If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

            S 1 Reply Last reply
            1
            • eyllanescE eyllanesc

              @JonB

              paint receives the option but then modifies it by invoking the initStyleOption. If you want to know how a class works then it is better to check the source code: https://code.qt.io/cgit/qt/qtbase.git/tree/src/widgets/itemviews/qstyleditemdelegate.cpp?h=dev#n377

              void QStyledItemDelegate::paint(QPainter *painter,
                      const QStyleOptionViewItem &option, const QModelIndex &index) const
              {
                  Q_ASSERT(index.isValid());
              
                  QStyleOptionViewItem opt = option;
                  initStyleOption(&opt, index); // <--- update option
              
                  const QWidget *widget = QStyledItemDelegatePrivate::widget(option);
                  QStyle *style = widget ? widget->style() : QApplication::style();
                  style->drawControl(QStyle::CE_ItemViewItem, &opt, painter, widget);
              }
              

              @sawarsi you should not modify the paint method

              S Offline
              S Offline
              sawarsi
              wrote on last edited by
              #6

              @eyllanesc yes i removed the paint function entirely and used your suggestion and i am still seeing ellipses.

              eyllanescE 1 Reply Last reply
              0
              • S sawarsi

                @eyllanesc yes i removed the paint function entirely and used your suggestion and i am still seeing ellipses.

                eyllanescE Offline
                eyllanescE Offline
                eyllanesc
                wrote on last edited by
                #7

                @sawarsi Could you provide a minimum reproducible example? Maybe the error is another.

                If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

                S 3 Replies Last reply
                0
                • eyllanescE eyllanesc

                  @sawarsi Could you provide a minimum reproducible example? Maybe the error is another.

                  S Offline
                  S Offline
                  sawarsi
                  wrote on last edited by
                  #8

                  @eyllanesc ok i will see if i can setup a simple example

                  JonBJ S 3 Replies Last reply
                  0
                  • S sawarsi

                    @eyllanesc ok i will see if i can setup a simple example

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

                    @sawarsi Also before you start put a breakpoint/debug message into your code to make 100% sure your override is getting called.

                    S 2 Replies Last reply
                    0
                    • JonBJ JonB

                      @sawarsi Also before you start put a breakpoint/debug message into your code to make 100% sure your override is getting called.

                      S Offline
                      S Offline
                      sawarsi
                      wrote on last edited by
                      #10

                      @JonB yes it is getting hit.

                      1 Reply Last reply
                      1
                      • JonBJ JonB

                        @sawarsi Also before you start put a breakpoint/debug message into your code to make 100% sure your override is getting called.

                        S Offline
                        S Offline
                        sawarsi
                        wrote on last edited by
                        #11

                        @JonB what i did notice in the debugger is that each of the cells in the second column i have put in text but when i print the text in initStyleOption then it is always "".

                        so is setItemDelegateForColumn(1,...) the correct way of doing this?

                        1 Reply Last reply
                        0
                        • S sawarsi

                          @eyllanesc ok i will see if i can setup a simple example

                          S Offline
                          S Offline
                          sawarsi
                          wrote on last edited by
                          #12

                          @sawarsi nevermind it was my fault your suggestion is flawless.

                          1 Reply Last reply
                          0
                          • eyllanescE eyllanesc

                            @sawarsi Could you provide a minimum reproducible example? Maybe the error is another.

                            S Offline
                            S Offline
                            sawarsi
                            wrote on last edited by
                            #13

                            @eyllanesc thanks for the help.

                            1 Reply Last reply
                            0
                            • S sawarsi

                              @eyllanesc ok i will see if i can setup a simple example

                              S Offline
                              S Offline
                              sawarsi
                              wrote on last edited by
                              #14

                              @sawarsi one thing i did notice is that this works just fine in linux but fails on windows any hints?

                              eyllanescE 1 Reply Last reply
                              0
                              • eyllanescE eyllanesc

                                @sawarsi Could you provide a minimum reproducible example? Maybe the error is another.

                                S Offline
                                S Offline
                                sawarsi
                                wrote on last edited by
                                #15

                                @eyllanesc one thing i did notice is that this works just fine in linux but fails on windows any hints?

                                1 Reply Last reply
                                0
                                • S sawarsi

                                  @sawarsi one thing i did notice is that this works just fine in linux but fails on windows any hints?

                                  eyllanescE Offline
                                  eyllanescE Offline
                                  eyllanesc
                                  wrote on last edited by
                                  #16

                                  @sawarsi As can be seen in the source code of "paint", the QStyle is used to make the painting, so the style probably does not use some option properties, a possible solution is to use another style like: app.setStyle("fusion" );

                                  If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

                                  S 1 Reply Last reply
                                  0
                                  • eyllanescE eyllanesc

                                    @sawarsi As can be seen in the source code of "paint", the QStyle is used to make the painting, so the style probably does not use some option properties, a possible solution is to use another style like: app.setStyle("fusion" );

                                    S Offline
                                    S Offline
                                    sawarsi
                                    wrote on last edited by
                                    #17

                                    @eyllanesc i tried all the Windows styles (Windows and Fusion) and neither worked it may be that Windows mandates ellipses. i am not sure anyway, i am going to mark this solved.

                                    1 Reply Last reply
                                    0
                                    • eyllanescE eyllanesc

                                      @sawarsi You have to override the initStyleOption method

                                      class StyledItemDelegate: public QStyledItemDelegate
                                      {
                                      public:
                                          using QStyledItemDelegate::QStyledItemDelegate;
                                      protected:
                                          void initStyleOption(QStyleOptionViewItem *option, const QModelIndex &index) const override
                                          {
                                              QStyledItemDelegate::initStyleOption(option, index);
                                              option->textElideMode = Qt::ElideNone;
                                          }
                                      };
                                      
                                      C Offline
                                      C Offline
                                      CEO.
                                      wrote on last edited by
                                      #18

                                      @eyllanesc I admire you a lot. This is one of the best ways for an informed person to respond to a question. Anyone asking question is usually confused and an illustrative answer is the best. I notice you always go out of your way to give a practical example. Honestly if you have a class where you teach students, I would love to be one of your students.

                                      Thank you very much.

                                      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