Put a QLabel on anothers QLabels



  • Hello!!

    I am trying to do something more visual that I have. I want to put a QLabel on anothers QLabels like this picture:

    0_1517213203471_0c3f24ad-14bc-4842-90b3-504c6554e4a9-imagen.png

    Each color is a QLabel and I want to put text on this labels.

    How coul I do it?

    Thank you very much!!!


  • Moderators

    @ivanicy
    does it really have to be a composition of labels?

    Depending on your requirements it would be easier (at least less code) if you just do the painting (on a custom QFrame widget):

    void MyFrame::paintEvent( QPaintEvnet* event )
    {
        QFrame::paintEvent( event );
    
        QPainter p(this);
           p.setRenderHints( QPainter::TextAntialiasing | QPainter::Antialiasing );
    
       const int sectionWidth = this->contentsRect().width() / 7;
       const int sectionHeight = this->contentsRect().height();
    
       // draw color rects
       p.setBrush( Qt::yellow );
       p.drawRect( 0 * sectionWidth , 0 , sectionWidth, sectionHeight );
    
       p.setBrush( Qt::blue );
       p.drawRect( 1 * sectionWidth , 0 , sectionWidth, sectionHeight );
    
       p.setBrush( Qt::green );
       p.drawRect( 2 * sectionWidth , 0 , sectionWidth, sectionHeight );
       .....
    
       // draw text
       p.setFont( ... );
       p.setPen( Qt::white );
       p.drawText( this->contentsRect(), Qt::AlignCenter, "LabelText" );
    }
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.