简体   繁体   中英

QT- choose color from combobox and draw rectangle

I want to choose the color from the dropdown and based on that color I want to draw a rectangle on the window. I can draw a rectangle with predefined color but not sure how can I pass the color from the combobox. and Only one rectangle is drawn on the window, I want to draw multiple rectangles on the window.

So the procedure works like this. User will click the push button --> combobox appears ---> choose the color --> click OK and rectangle of that color will appear on the window.

Dialog.cpp

    Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    ui->setupUi(this);
}

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

class CustomDialog : public QDialog
{
public:
    CustomDialog(const QStringList& items)
    {
        setLayout(new QHBoxLayout());

        box = new QComboBox;
        box->addItems(items);
        layout()->addWidget(box);
        connect(box, SIGNAL(currentIndexChanged(const QString&)), this, SLOT(colorSelected(const QString&)));
        QPushButton* ok = new QPushButton("ok");
        layout()->addWidget(ok);
        connect(ok, &QPushButton::clicked, this, [this]()
        {
           accept();
        });
    }

    QComboBox* combobox() { return box; }

private:
    QComboBox* box;
};

void Dialog::on_pushButton_clicked()
{
    QStringList itemList({"Red", "Blue", "Green"});
    CustomDialog dialog(itemList);
   // connect(box, SIGNAL(currentIndexChanged(const QString&)), this, SLOT(colorSelected(const QString&)));
    if (dialog.exec() == QDialog::Accepted)
    {

        scene = new QGraphicsScene(this);
        ui->graphicsView->setScene(scene);
        QBrush redBrush(Qt::red);
        QBrush blackBrush(Qt::black);
        QPen blackpen(Qt::black);
        blackpen.setWidth(3);
        rectangle = scene->addRect(10,10,100,100,blackpen,redBrush);
        rectangle->setFlag(QGraphicsItem::ItemIsMovable);
    }
}

void Dialog::colorSelected(const QString& text)
{
      const QColor selected =  colorMap[text];
}

The previous post doesnt solve my question.

If you are not interested in a color picker and you want your own solution with a QComboBox you can predefine your own set of colors you want to use.

You can declare a map, for instance QMap<QString, QColor> colors; which would hold the color value under a key which describes it with a string.

Then you should define the desired values. For example:

colors = 
{
    {"Red", QColor(255, 0, 0)},
    {"Green", QColor(0, 255, 0)},
    {"Blue", QColor(0, 0, 255)},
};

You can easily use this map to fill the contents of your QComboBox . That's quick as writing:

box->addItems(colors.keys());

Next action depends on when you need to know the color value. If you want to know it right after user selects it you would need to make a proper connect . For example:

connect(box, SIGNAL(currentIndexChanged(const QString&)), this, SLOT(colorSelected(const QString&)));

Where colorSelected would be the slot where you handle the selected color value:

void Dialog::colorSelected(const QString& text)
{
    const QColor selected = colors[text];
    // do what you need with the color
}

In addition to the above, I'd suggest using the item data to hold the color definitions. Look at:

QComboBox::setItemData
QComboBox::itemData
QComboBox::currentData

Each item in a combo box has two components; there's the text that's displayed, and then there's a related QVariant, which is the item data. The QVariant can hold your color definition so that you don't have to store them separately. In reality, it's more than two because you could define as many user roles as you want, which would allow you to store even more data elements per combobox item. For your purposes, however, the default UserRole is sufficient.

Your colorSelected definition would be:

void Dialog::colorSelected (const QString &text)
{
    const QColor selected = box->currentData().value <QColor> ();
    // do what you need with the color
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM