简体   繁体   English

QT-从组合框中选择颜色并绘制矩形

[英]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 对话框.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. 如果您对颜色选择器不感兴趣,并且希望使用QComboBox解决方案,则可以预定义要使用的颜色集。

You can declare a map, for instance QMap<QString, QColor> colors; 您可以声明一个地图,例如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 . 您可以轻松地使用此地图来填充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 . 如果要在用户选择后立即知道它,则需要进行适当的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: 其中colorSelected将是您处理所选颜色值的插槽:

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. 有显示的文本,还有相关的QVariant,即商品数据。 The QVariant can hold your color definition so that you don't have to store them separately. QVariant可以保留您的颜色定义,因此您不必单独存储它们。 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. 但是,出于您的目的,默认的UserRole就足够了。

Your colorSelected definition would be: 您的colorSelected定义为:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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