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 can I modify the appearance of a QTableView?
Forum Updated to NodeBB v4.3 + New Features

How can I modify the appearance of a QTableView?

Scheduled Pinned Locked Moved General and Desktop
10 Posts 8 Posters 19.2k 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.
  • J Offline
    J Offline
    Jonathan
    wrote on last edited by
    #1

    How can I modify the appearance of a QTableView? For instance by changing the background colour of a column, or changing the colours or fonts used in row and column headers?

    (see http://doc.qt.nokia.com/4.7/sql-cachedtable.html)

    1 Reply Last reply
    0
    • M Offline
      M Offline
      milot.shala
      wrote on last edited by
      #2

      You can use Qt style sheets for customizing the table view, you can see more detailed description how to so here: "http://doc.trolltech.com/latest/stylesheet-examples.html#customizing-qtableview":http://doc.trolltech.com/latest/stylesheet-examples.html#customizing-qtableview

      You can use also the setAlternatingRowColors(bool); to set the alternate colors, which it has a default color but you can change it via the style sheets using the following style sheet syntax within the setStyleSheet(QString) function:

      @
      QTableView { alternate-background-color: #ebf4ff }
      @

      1 Reply Last reply
      0
      • B Offline
        B Offline
        blex
        wrote on last edited by
        #3

        You can use delegate: http://doc.qt.nokia.com/4.7/qstyleditemdelegate.html


        Oleksiy Balabay

        1 Reply Last reply
        0
        • D Offline
          D Offline
          DenisKormalev
          wrote on last edited by
          #4

          In addition to blex. If you need to have some rich text in your table than you can use QTextDocument in delegates. It will do all stuff for you.

          1 Reply Last reply
          0
          • I Offline
            I Offline
            infoctopus
            wrote on last edited by
            #5

            Jonathan,

            QTableView look can be modified in a number of ways:

            1. Background color of a column, text color or font used in items or headers can be specified in a model, that your QTableView is connected to:

            @QVariant QAbstractItemModel::data ( const QModelIndex & index, int role = Qt::DisplayRole ) const [pure virtual]@

            Implement this function in your model and return different values depending on values of role and index (i.e. row, column).

            Similarly you can define look for headers. Use yourModel::headerData()
            @QVariant QAbstractItemModel::headerData ( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const [virtual]@

            1. Use style-sheets. Refer to Qt Style Sheets Reference / QTableView in Assistant

            2. Use Delegates. See Model/View Programming in Assistant

            3. Use custom widgets as items via QAbstractItemView::setIndexWidget()

            4. There other ways, but they are just inapropriate for the defined task

            I'd suggest using 1 or 2 or 3

            Qt rulez

            1 Reply Last reply
            0
            • D Offline
              D Offline
              DBoosalis
              wrote on last edited by
              #6

              Here is an example that shows how I use a delegate to paint pixmaps in a cell which is dependent on the value of some user data.

              Hope this example is of some help.

              @
              void ModelDelegate::paint(QPainter *painter,
              const QStyleOptionViewItem &option,
              const QModelIndex &index) const
              {

              QStyledItemDelegate::paint(painter,option,index);
              QStandardItemModel *m = (QStandardItemModel *) index.model();
              StateItem *si = (StateItem *) m->itemFromIndex(index);

              QVariant v = index.data(Qt::UserRole+2);
              QWidget *w = qobject_cast<QWidget *>( (QWidget *) v.value<void *>());
              if (!w) {
              qWarning() << "No widget " << FILE << LINE;
              return;
              }
              int x2,y2;
              QRect rect = option.rect;
              if (w->isVisible()) {
              x2 = rect.x() + rect.width() - visiblePixmap->width()-4;
              y2 = rect.y() + (rect.height()- visiblePixmap->height())/2;
              painter->drawPixmap(x2,y2,visiblePixmap);
              }
              if (si->symbolFilterOn) {
              x2 = rect.x() + rect.width() - (2
              visiblePixmap->width())-4;
              y2 = rec.y() + (rect.height()- filterPixmap->height())/2;
              painter->drawPixmap(x2,y2,*filterPixmap);
              }

              }
              @

              [edit : code highlighted / Denis Kormalev]

              1 Reply Last reply
              0
              • D Offline
                D Offline
                DenisKormalev
                wrote on last edited by
                #7

                DBoosalis, please use @ tags around your code.

                1 Reply Last reply
                0
                • J Offline
                  J Offline
                  Jonathan
                  wrote on last edited by
                  #8

                  All,

                  Many thanks for your extremely helpful responses. These were just what I needed.

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    andre
                    wrote on last edited by
                    #9

                    Note that adapting the style of the headers takes more effort. You'll have to resort to subclassing QHeaderView and reimplementing paint().

                    1 Reply Last reply
                    0
                    • I Offline
                      I Offline
                      Irfan
                      wrote on last edited by
                      #10

                      If you want to do more c++ Qt codeing then Delegate is there to manipulate further. You can change the color of particular cell on some condition, you can have your own cell (cutstomized widget cell) and what not ....?
                      There are hell of the text available to read more about it i am sorry but it is really a bit complex too.
                      http://doc.qt.nokia.com/4.1/model-view-delegate.html
                      You can google further for examples.

                      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