Search code examples
c++qtwidgetqpainter

how to draw lines able to change color in Qt?


there is a question disturbing me, maybe it's just a simple question for you guys. i want to draw tow lines in my Qt widget, when the QComboBox cbx is set at the item 1, the first line is set to red, second black, on the contrary, 2, fist black, second red. i used *paintEvent* to draw my lines. (actually, if you want to draw something, you must draw in paintEvent function) But i dont know how to change the line's color based on the QComboBox item, follows are my codes.

#include "changecolor.h"  
#include "ui_changecolor.h"  
QString st;  
QPainter painter;  

ChangeColor::ChangeColor(QWidget *parent) :  
    QMainWindow(parent),  
    ui(new Ui::ChangeColor)  
{  
    ui->setupUi(this);  
    setFixedSize(2000, 1000);  
    QComboBox * cbx = new QComboBox(this);  
    cbx->setSizeAdjustPolicy(QComboBox::AdjustToContents);  
    cbx->addItem("1", Qt::DisplayRole);  
    cbx->addItem("2", Qt::DisplayRole);  
    st = cbx->currentText();  
    connect(cbx, SIGNAL(currentTextChanged(QString)), this, SIGNAL(changeColorSlot(st, painter)));  
}  

void ChangeColor::paintEvent(QPaintEvent*)  
{  
//    QPainter painter(this);  
//    painter.setPen(Qt::black);  
//    painter.drawLine(QPoint(100,100), QPoint(1100,100));  
//    painter.drawLine(QPoint(100,100), QPoint(100,600));  
//    changeColorSlot(painter, );  
    changeColorSlot(st, painter);  
}  

void ChangeColor::changeColorSlot(QString& st, QPainter& painter)  
{  
//    QPainter painter(this);  
    if(st == tr("1"))  
    {  
        painter.setPen(Qt::black);  
        painter.drawLine(QPoint(100,100),QPoint(1100,100));  
        painter.setPen(Qt::red);  
        painter.drawLine(QPoint(100,100),QPoint(100,600));  
    }  
    else if(st == tr("2"))  
    {  
        painter.setPen(Qt::red);  
        painter.drawLine(QPoint(100,100),QPoint(1100,100));  
        painter.setPen(Qt::black);  
        painter.drawLine(QPoint(100,100),QPoint(100,600));  
    }  
    update();  

}  

ChangeColor::~ChangeColor()  
{  
    delete ui;  
}  

these codes denote my painful coding life, i mean i have tried many times, but there is no right result. thank you guys.


Solution

  • you have to set the logic that controls the state(the color of the lines) outside the widget:

    here an example with the relevant parts

    .h: #include

    namespace Ui
    {
        class ChangeColor;
    }
    
    class ChangeColor : public QWidget
    {
        Q_OBJECT
    
        public:
            explicit ChangeColor(QWidget *parent = nullptr);
            void paintEvent(QPaintEvent* pe);
            ~ChangeColor();
    
        public slots:
            void setColorState(int state);
    
        private:
            Ui::ChangeColor* ui{nullptr};
            int state{};
    };
    
    .cpp
    ...
    void ChangeColor::paintEvent(QPaintEvent *event)
    {
        Q_UNUSED(event)
        QPainter paint(this);
    
        QColor RED_COLOR{255, 0, 0};
        QColor BLACK_COLOR{0, 0, 0};
        if(state == 0)
        {
            paint.setPen(QPen(RED_COLOR));
            paint.setBrush(RED_COLOR);
        }
        else
        {
            paint.setPen(QPen(BLACK_COLOR));
            paint.setBrush(BLACK_COLOR);
        }
        paint.drawLine(0, 0, width(), height());
    }
    
    void ChangeColor::setColorState(int state)
    {
        this->state = state;
        update();
    }