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. QSqlQueryModel With QTableView
Forum Updated to NodeBB v4.3 + New Features

QSqlQueryModel With QTableView

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 2.5k 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.
  • S Offline
    S Offline
    Shiv
    wrote on 28 May 2016, 11:00 last edited by
    #1

    Dear All

    I have implemented a QTableView with QSqlQueryModel and I am able to display the data , but I am unable to align the text to center.The text is align to left by default.
    Some of the post says that the QSqlQueryModel has to be subclassed Is there any option without subclassing the text can be aligned in the tableview if not can any one provide a small subclassed example to start with .

    Thanks & Regards
    Shiv

    R 1 Reply Last reply 28 May 2016, 11:50
    0
    • P Offline
      P Offline
      panosk
      wrote on 28 May 2016, 11:32 last edited by
      #2

      Hi,
      You should reimplement the data() method and return the alignment you want, like this:

      QVariant YourModel::data(const QModelIndex &item, int role) const
      {
      
      if(!item.isValid())
          return QVariant();
      
      if (item.column() == /*The column number where your text appears*/) {
          if (role == Qt::QtTextAlignmentRole)
               return Qt::AlignCenter;
      }
               
      return QSqlQueryModel::data(item,role);
      }
      

      Otherwise I think you need to provide a custom delegate and manipulate text in there, but I don't think you'll save more time or trouble.

      1 Reply Last reply
      0
      • S Shiv
        28 May 2016, 11:00

        Dear All

        I have implemented a QTableView with QSqlQueryModel and I am able to display the data , but I am unable to align the text to center.The text is align to left by default.
        Some of the post says that the QSqlQueryModel has to be subclassed Is there any option without subclassing the text can be aligned in the tableview if not can any one provide a small subclassed example to start with .

        Thanks & Regards
        Shiv

        R Offline
        R Offline
        Ratzz
        wrote on 28 May 2016, 11:50 last edited by
        #3

        @Shiv
        Here is a sub classing using QAbstractTableModel and override data() like this .

        --Alles ist gut.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          Shiv
          wrote on 28 May 2016, 12:08 last edited by
          #4

          Hi @panosk ,@Ratzz
          Thanks for your kind reply.I was trying to subclass the QSqlQueryModel to reimplement the data function but I am bit confused with the constructor given in the class document ,by default the active database will be connected or we have to pass a database to get connected.If possible can you post a simple class example with only its constructor and initialization.

          Thanks
          Shiv

          1 Reply Last reply
          0
          • S Offline
            S Offline
            Shiv
            wrote on 28 May 2016, 12:34 last edited by
            #5

            Hi

            I got the example from http://doc.qt.io/qt-5/qtsql-querymodel-example.html

            Thank you
            Shiv

            1 Reply Last reply
            0

            1/5

            28 May 2016, 11:00

            • Login

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