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. [Solved] Text alignment in QTableView cell
Forum Updated to NodeBB v4.3 + New Features

[Solved] Text alignment in QTableView cell

Scheduled Pinned Locked Moved General and Desktop
17 Posts 4 Posters 45.8k Views 1 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.
  • Q Offline
    Q Offline
    qxoz
    wrote on last edited by
    #7

    Hi!
    I think it's works only for QStandardItemModel. As Andre says:
    [quote author="Andre" date="1322127930"]You can do these things from two sides: from the model side (including using a proxy model) or from the delegate side. All approaches unfortunately involve subclassing, unless you are using something like QStandardItemModel. [/quote]
    But i am not using Qt 5 for now and maybe something changed there.

    1 Reply Last reply
    0
    • Q Offline
      Q Offline
      qxoz
      wrote on last edited by
      #8

      [quote author="mbnoimi" date="1366731763"]
      PS
      I don't want to do any subclassing I'm looking for easy bezy solution[/quote]
      By the way subclassing is not so terrifying as you can imagine at first time, after some practice it will be easy bezy and most convinient way.

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mbnoimi
        wrote on last edited by
        #9

        bq. But i am not using Qt 5 for now and maybe something changed there.

        Could you please post a snippet explains the solution you've reached to in Qt4?

        bq. By the way subclassing is not so terrifying as you can imagine at first time, after some practice it will be easy bezy and most convinient way.

        I agree with you. subclassing not terrifying but for rapid developing and simple tools it force me to write more codes which means more time.

        A solution without subclassing will be more easy to write and simple to deal with.

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

          Sometimes you just can't avoid to subclass and this case, it really isn't that terrible.

          You can either subclass your model or use a "QIdentityProxyModel":http://qt-project.org/doc/qt-4.8/qidentityproxymodel.html#details

          @
          class DateFormatProxyModel : public QIdentityProxyModel
          {
          ..//Add constructor

          QVariant data(const QModelIndex &index, int role)
          {
          if (role == Qt::TextAlignmentRole) {
          return Qt::AlignRight;
          }
          return QIdentityProxyModel::data(index, role);
          }

          };@

          The same can be done subclassing the model you are using.

          Hope it helps

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

          1 Reply Last reply
          1
          • M Offline
            M Offline
            mbnoimi
            wrote on last edited by
            #11

            I came from the death heheheh:

            I tried to use QIdentityProxyModel as you mentioned above but my app unexpectedly finish!!!

            How can I use QIdentityProxyModel with QTableView?

            [code]#include <QIdentityProxyModel>

            class MyProxyModel : public QIdentityProxyModel
            {
            public:
            MyIdentityModel(QObject* parent = 0): QIdentityProxyModel(parent)
            {
            qDebug() << "Do nothin!";
            }
            QVariant data(const QModelIndex &index, int role) const
            {
            if (role == Qt::TextAlignmentRole) {
            return Qt::AlignCenter;
            }
            return QIdentityProxyModel::data(index, role);
            }
            };[/code]

            Call it for QTableView:

            [code]
            QSqlTableModel *_tableModel = new QSqlTableModel(0, db);
            _tableModel->setTable(table);
            _tableModel->select();
            MyProxyModel *_proxy;
            _proxy->setSourceModel(_tableModel);
            ui->tableView->setModel(_proxy);
            [/code]

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

              You need to create it first:

              @MyProxyModel *_proxy = new MyProxyModel;@

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

              1 Reply Last reply
              0
              • M Offline
                M Offline
                mbnoimi
                wrote on last edited by
                #13

                [quote author="SGaist" date="1395262620"]MyProxyModel *_proxy = new MyProxyModel;[/quote]

                Didn't fix the issue... I got this error when I added it:

                [code]In file included from ../QTableViewRTL/mainwindow.h:10:0,
                from ../QTableViewRTL/mainwindow.cpp:1:
                ../QTableViewRTL/myproxymodel.h:9:40: error: ISO C++ forbids declaration of 'MyIdentityModel' with no type [-fpermissive]
                MyIdentityModel(QObject* parent = 0): QIdentityProxyModel(parent)
                ^
                ../QTableViewRTL/myproxymodel.h: In member function 'int MyProxyModel::MyIdentityModel(QObject*)':
                ../QTableViewRTL/myproxymodel.h:9:43: error: only constructors take member initializers
                MyIdentityModel(QObject* parent = 0): QIdentityProxyModel(parent)
                ^
                ../QTableViewRTL/myproxymodel.h:12:5: warning: no return statement in function returning non-void [-Wreturn-type]
                }
                ^
                make: *** [mainwindow.o] Error 1
                23:01:45: The process "/usr/bin/make" exited with code 2.
                Error while building/deploying project QTableViewRTL (kit: Desktop Qt 5.2.1 GCC 64bit)
                When executing step 'Make'
                23:01:45: Elapsed time: 00:02.[/code]

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

                  It fixes your crashing issue.

                  Now the compiler error is something else. Correct the constructor name, your class is called MyProxyModel and your are using MyIdentityModel as constructor name.

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

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    mbnoimi
                    wrote on last edited by
                    #15

                    Oops my bad... Thank you.

                    1 Reply Last reply
                    0
                    • Q Offline
                      Q Offline
                      qxoz
                      wrote on last edited by
                      #16

                      Can i mark the thread as solved? :)

                      1 Reply Last reply
                      0
                      • M Offline
                        M Offline
                        mbnoimi
                        wrote on last edited by
                        #17

                        [quote author="qxoz" date="1395313861"]Can i mark the thread as solved? :)[/quote]

                        Yes please.

                        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