我想从下拉列表中选择颜色,并基于该颜色我想在窗口上绘制一个矩形。 我可以用预定义的颜色绘制一个矩形,但不知道如何从组合框中传递颜色。 和只有一个矩形绘制在窗口上,我想在窗口上绘制多个矩形。QT-从组合框中选择颜色并绘制矩形
所以程序就是这样工作的。用户将点击按钮 - >组合框出现--->选择颜色 - >单击确定,该颜色的矩形将出现在窗口上。
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];
}
上一篇犯规解决我的问题。
OT:wwWidgets有一个漂亮的颜色选择组合框。 http://www.wysota.eu.org/wwwidgets/ doc:http://www.wysota.eu.org/wwwidgets/doc/html/qwwcolorcombobox.html – drescherjm
我有我想要使用的预定义颜色。我只是想根据从QT – PSDebugger
的下拉列表中选择的颜色创建一个矩形,并且有什么问题?从对话框中获取选定的颜色并使用它。 – ilotXXI