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 24 Nov 2011, 08:55 last edited by
    #1

    Is there any way align data shown in cells of QTableWiev without reimplementing of model's methods?
    I mean some property like setAlign.

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on 24 Nov 2011, 09:45 last edited by
      #2

      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.

      1 Reply Last reply
      0
      • Q Offline
        Q Offline
        qxoz
        wrote on 24 Nov 2011, 10:01 last edited by
        #3

        I actually use QStandardItemModel.

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on 24 Nov 2011, 10:20 last edited by
          #4

          In that case, you can just call
          @
          myStandardItem->setData(Qt::AlignRight, Qt::TextAlignmentRole);
          @
          to align an item to the right side of the cell. Check the documentation on Qt::AlignmentFlag for other alignment options, and the Qt::ItemDataRole documentation for information on other interesting roles you may want to set.

          1 Reply Last reply
          0
          • Q Offline
            Q Offline
            qxoz
            wrote on 24 Nov 2011, 10:34 last edited by
            #5

            Thanks a lot!
            That is what i needed.

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mbnoimi
              wrote on 23 Apr 2013, 15:42 last edited by
              #6

              [quote author="Andre" date="1322130004"]In that case, you can just call
              @
              myStandardItem->setData(Qt::AlignRight, Qt::TextAlignmentRole);
              @
              to align an item to the right side of the cell. Check the documentation on Qt::AlignmentFlag for other alignment options, and the Qt::ItemDataRole documentation for information on other interesting roles you may want to set.[/quote]

              I face same issue here but it didn't work with me in Qt5 :( check this snippet please:

              [code]void MainWindow::on_pushButton_clicked()
              {
              pDB = QSqlDatabase::addDatabase("QSQLITE");
              pDB.setDatabaseName("./data.db");
              if (pDB.open()){
              QSqlTableModel *pTableModel = new QSqlTableModel(0, pDB);
              pTableModel->setTable("Jornal");
              pTableModel->select();

                 ui->tableView->setModel(pTableModel);
                 ui->tableView->horizontalHeader()->setDefaultAlignment(Qt::AlignCenter);
                 // BUG: This is absolute error!!!
                 model->setData(Qt::AlignRight, Qt::TextAlignmentRole);
              

              }
              else
              qDebug()<<"Error db";
              }[/code]

              PS
              I don't want to do any subclassing I'm looking for easy bezy solution

              1 Reply Last reply
              0
              • Q Offline
                Q Offline
                qxoz
                wrote on 24 Apr 2013, 05:51 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 24 Apr 2013, 05:56 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 24 Apr 2013, 07:52 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
                    • S Offline
                      S Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on 24 Apr 2013, 09:02 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 19 Mar 2014, 14:33 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
                        • S Offline
                          S Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on 19 Mar 2014, 20:57 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 19 Mar 2014, 21:03 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
                            • S Offline
                              S Offline
                              SGaist
                              Lifetime Qt Champion
                              wrote on 19 Mar 2014, 21:36 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 19 Mar 2014, 22:01 last edited by
                                #15

                                Oops my bad... Thank you.

                                1 Reply Last reply
                                0
                                • Q Offline
                                  Q Offline
                                  qxoz
                                  wrote on 20 Mar 2014, 11:11 last edited by
                                  #16

                                  Can i mark the thread as solved? :)

                                  1 Reply Last reply
                                  0
                                  • M Offline
                                    M Offline
                                    mbnoimi
                                    wrote on 20 Mar 2014, 11:27 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