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. Change painter in delegate

Change painter in delegate

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 702 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.
  • krzysieklfcK Offline
    krzysieklfcK Offline
    krzysieklfc
    wrote on last edited by
    #1

    Hi,

    I want to change the font when a cell in QTableView is double clicked, while preserving all the other formatting.
    I thought I could simply supply custom painter to the default implementation, but it doesn't change anything.

    void TableViewDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option,
    	const QModelIndex& index) const
    {
    	QStyleOptionViewItem opt{ option };
    	initStyleOption(&opt, index);
    
    	if (index.row() == m_doubleclicked_row) {
    		auto my_painter = QPainter{};
    		my_painter.setPen(Qt:green);
    		QStyledItemDelegate::paint(&my_painter, opt, index);
    	}
    }
    
    

    How can I solve this?

    1 Reply Last reply
    0
    • krzysieklfcK krzysieklfc

      @SGaist The following renders new text behind the original instead of changing it. Also, is there a way to set just the font color with this method?

      	if (index.row() == m_doubleclicked_row) {
      		opt.font = QFont{ "Papyrus", 11 };
      		QStyledItemDelegate::paint(painter, opt, index);
      	}
      
      mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #6

      @krzysieklfc
      Hi
      I think you have 2 calls to QStyledItemDelegate::paint then.
      can you show whole paitnEvent code?
      Maybe you just need return in the IF statement so it dont draw twice.

      To change color, you can change the palette

      opt.palette.setColor(QPalette::HighlightedText,Qt::red);
      opt.palette.setColor(QPalette::Text,Qt::red);

      1 Reply Last reply
      2
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by mrjj
        #2

        Hi
        The QStyledItemDelegate will use the FontRole to paint the text so setting
        the prior will not help.
        Since the painter function is const we cannot set it there so
        maybe you could hook up to the cell double click signal and set the models
        data for that cell to include a font that is bold
        something like

         connect( this, &QListView::doubleClicked, this, [this](const QModelIndex & index) {
                QFont font(index.data(Qt::FontRole).value<QFont>());
                font.setBold(true);
                model()->setData(index, font, Qt::FontRole) ;
            });
        
        
        1 Reply Last reply
        1
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #3

          Hi,

          Since you already have a copy of option why not modify the font variable of it ?

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

          krzysieklfcK 1 Reply Last reply
          2
          • mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #4

            Hi
            Using QStyleOptionViewItem opt{ option }; is a good idea but you then need
            to keep of which index that was doubled clicked.

            Do you need to later turn it back to non bold, you should it stay bold forever after being db clicked ?

            1 Reply Last reply
            1
            • SGaistS SGaist

              Hi,

              Since you already have a copy of option why not modify the font variable of it ?

              krzysieklfcK Offline
              krzysieklfcK Offline
              krzysieklfc
              wrote on last edited by
              #5

              @SGaist The following renders new text behind the original instead of changing it. Also, is there a way to set just the font color with this method?

              	if (index.row() == m_doubleclicked_row) {
              		opt.font = QFont{ "Papyrus", 11 };
              		QStyledItemDelegate::paint(painter, opt, index);
              	}
              
              mrjjM 1 Reply Last reply
              0
              • krzysieklfcK krzysieklfc

                @SGaist The following renders new text behind the original instead of changing it. Also, is there a way to set just the font color with this method?

                	if (index.row() == m_doubleclicked_row) {
                		opt.font = QFont{ "Papyrus", 11 };
                		QStyledItemDelegate::paint(painter, opt, index);
                	}
                
                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #6

                @krzysieklfc
                Hi
                I think you have 2 calls to QStyledItemDelegate::paint then.
                can you show whole paitnEvent code?
                Maybe you just need return in the IF statement so it dont draw twice.

                To change color, you can change the palette

                opt.palette.setColor(QPalette::HighlightedText,Qt::red);
                opt.palette.setColor(QPalette::Text,Qt::red);

                1 Reply Last reply
                2

                • Login

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