Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. How to set current cell color in a tableview and how to set text centerAligned
QtWS25 Last Chance

How to set current cell color in a tableview and how to set text centerAligned

Scheduled Pinned Locked Moved Solved C++ Gurus
4 Posts 2 Posters 2.6k Views
  • 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.
  • M Offline
    M Offline
    moyin
    wrote on last edited by
    #1

    Actually i'm trying with QStandardItemModel Approach with QT5.
    Please help me if any one hav done this before.

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      If you're using QStandardItemModel alignment can be set directly on the item when you create it:

      auto item = new QStandardItem("Hello");
      item->setTextAlignment(Qt::AlignCenter);
      

      You can also set the foreground/background color of the cell with

      item->setForeground(Qt::red);
      item->setBackground(Qt::blue);
      

      If you want to change the color of the selected item you can use a stylesheet:

      table_view->setStyleSheet("QTableView { selection-color: yellow; selection-background-color: green; }");
      
      M 2 Replies Last reply
      1
      • Chris KawaC Chris Kawa

        If you're using QStandardItemModel alignment can be set directly on the item when you create it:

        auto item = new QStandardItem("Hello");
        item->setTextAlignment(Qt::AlignCenter);
        

        You can also set the foreground/background color of the cell with

        item->setForeground(Qt::red);
        item->setBackground(Qt::blue);
        

        If you want to change the color of the selected item you can use a stylesheet:

        table_view->setStyleSheet("QTableView { selection-color: yellow; selection-background-color: green; }");
        
        M Offline
        M Offline
        moyin
        wrote on last edited by
        #3

        @Chris-Kawa
        thanks for replying.
        i'm using QmodelIndex to set data into cell not QstandardItem.
        than how to do the same

        1 Reply Last reply
        0
        • Chris KawaC Chris Kawa

          If you're using QStandardItemModel alignment can be set directly on the item when you create it:

          auto item = new QStandardItem("Hello");
          item->setTextAlignment(Qt::AlignCenter);
          

          You can also set the foreground/background color of the cell with

          item->setForeground(Qt::red);
          item->setBackground(Qt::blue);
          

          If you want to change the color of the selected item you can use a stylesheet:

          table_view->setStyleSheet("QTableView { selection-color: yellow; selection-background-color: green; }");
          
          M Offline
          M Offline
          moyin
          wrote on last edited by
          #4

          @Chris-Kawa
          thanks chris,
          i got it thanks a lot.

          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