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. How to Add unit with data in columns ?
Forum Updated to NodeBB v4.3 + New Features

How to Add unit with data in columns ?

Scheduled Pinned Locked Moved Unsolved General and Desktop
13 Posts 4 Posters 1.4k 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.
  • N Offline
    N Offline
    n-2204
    wrote on last edited by
    #1

    Hi,
    1->How to add unit or text with data in tableview column ?
    QAbstractItemModel* table1 = ui.tableView->model();
    table1->setData(table1->index(0, 1), speed + QString("rpm"));
    as i try this then rpm is coming but speed value come as some special character.
    2-> One column is user editable so when user enter any value then unit should add in with numeric value.
    3->as 2 same i need to do for a Qlineedit when user enter value unit should add.
    for qlineedit i tried setinputmask()
    ui.lineEdit_4->setInputMask("0000 unit"); // this is only way for lineedit
    How to do this?

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by VRonin
      #2

      How to add unit or text with data in tableview column ?

      Subclass QStyledItemDelegate and reimplement displayText. Something like:

      QString displayText(const QVariant &value, const QLocale &locale) const override{
      return QStyledItemDelegate::displayText(value,locale) + QStringLiteral(" rpm");
      }
      

      same i need to do for a Qlineedit

      Don't use lineedit, use QSpinBox/QDoubleSpinBox (you can call spinBox->setButtonSymbols(QAbstractSpinBox::NoButtons) if you don't want the buttons). They already have support for prefix and suffix

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      N 1 Reply Last reply
      5
      • VRoninV VRonin

        How to add unit or text with data in tableview column ?

        Subclass QStyledItemDelegate and reimplement displayText. Something like:

        QString displayText(const QVariant &value, const QLocale &locale) const override{
        return QStyledItemDelegate::displayText(value,locale) + QStringLiteral(" rpm");
        }
        

        same i need to do for a Qlineedit

        Don't use lineedit, use QSpinBox/QDoubleSpinBox (you can call spinBox->setButtonSymbols(QAbstractSpinBox::NoButtons) if you don't want the buttons). They already have support for prefix and suffix

        N Offline
        N Offline
        n-2204
        wrote on last edited by n-2204
        #3

        Thanks.. @VRonin where i can specify for which column ? displayText?

        1 Reply Last reply
        0
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #4

          Just call view->setItemDelegateForColumn to use the delegate in a specific column only

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          1 Reply Last reply
          0
          • N Offline
            N Offline
            n-2204
            wrote on last edited by
            #5

            for two column i have different suffix.
            for one column ->rpm column 9
            and for other-> hp ,column 8
            again need to reimplement displayText ?

            1 Reply Last reply
            0
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by
              #6

              you can make the suffix a property of your delegate:

              class SuffixDelegate : public QStyledItemDelegate{
              Q_OBJECT
              Q_DISABLE_COPY(SuffixDelegate)
              public:
              explicit SuffixDelegate(QObject* parent = nullptr) : QStyledItemDelegate(parent){}
              QString displayText(const QVariant &value, const QLocale &locale) const override{
              return QStyledItemDelegate::displayText(value,locale) + m_suffix;
              }
              void setSuffix(const QString& val) { m_suffix = val; }
              const QString& suffix() const {return m_suffix;}
              private:
              QString m_suffix;
              };
              

              Now you can:

              auto rpmDelegate = new SuffixDelegate(this);
              rpmDelegate->setSuffix(QStringLiteral(" rpm"));
              auto hpDelegate = new SuffixDelegate(this);
              hpDelegate->setSuffix(QStringLiteral(" hp"));
              view->setItemDelegateForColumn(9,rpmDelegate);
              view->setItemDelegateForColumn(8,hpDelegate );
              

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              1 Reply Last reply
              2
              • N Offline
                N Offline
                n-2204
                wrote on last edited by
                #7

                Thanks its working!!
                But i have dependency
                1->Table1 have 11 column in that i used integerdelegate for 6 column including the "column 8' in this column i need to set suffix also,
                2->and 4 column are non editable using noneditabledelegate, i need to set suffix for column 9 and make as non editable also.
                How can i do this ?

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Hi,

                  It's not the role of the delegate to decide what is editable or not, the model flag function is there for that reason.

                  What does your "noneditabledelegate" do ?

                  If you need several different delegates to support suffix, then derive them from the original one providing support for suffix.

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

                  N 1 Reply Last reply
                  2
                  • SGaistS SGaist

                    Hi,

                    It's not the role of the delegate to decide what is editable or not, the model flag function is there for that reason.

                    What does your "noneditabledelegate" do ?

                    If you need several different delegates to support suffix, then derive them from the original one providing support for suffix.

                    N Offline
                    N Offline
                    n-2204
                    wrote on last edited by
                    #9

                    What does your "noneditabledelegate" do ?

                    to make columns non editable

                    SGaistS 1 Reply Last reply
                    0
                    • N n-2204

                      What does your "noneditabledelegate" do ?

                      to make columns non editable

                      SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @n-2204 said in How to Add unit with data in columns ?:

                      What does your "noneditabledelegate" do ?

                      to make columns non editable

                      As already said, that's not the role of the delegate but the model flags method.

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

                      N 1 Reply Last reply
                      1
                      • SGaistS SGaist

                        @n-2204 said in How to Add unit with data in columns ?:

                        What does your "noneditabledelegate" do ?

                        to make columns non editable

                        As already said, that's not the role of the delegate but the model flags method.

                        N Offline
                        N Offline
                        n-2204
                        wrote on last edited by
                        #11

                        Thanks @SGaist ok i will read about flags

                        1 Reply Last reply
                        0
                        • N Offline
                          N Offline
                          n-2204
                          wrote on last edited by
                          #12

                          I have two Qtableview, in both tables need to make some columns as non editable so for both table I need to create a separate class to set model flags ?

                          JonBJ 1 Reply Last reply
                          0
                          • N n-2204

                            I have two Qtableview, in both tables need to make some columns as non editable so for both table I need to create a separate class to set model flags ?

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

                            @n-2204
                            Setting flags is done on the model, not the view. Delegates are set on the view, not the model.

                            If you prefer you can have your own separate sub-classes for either/both the models and the views. In that case you would indeed need separate classes for model flags/item delegates. (In the case of delegates you can also have separate sub-classes for the view but both using the same delegate class, if desired.)

                            Or, you can share the same model and/or view sub-class for both your tables, instead of having different dedicated ones. You would put into them some member field of your own to indicates which model/view each instance represents. Then you can use that in your flags or delegate methods to decide e.g. which columns to edit or hide based on which model/view your instance represents.

                            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