Hi there,
I'm building a GUI in C++ on Linux using Qt 5.9. I want to implement a functionality for choosing a color as part of a wizard. What I have in mind is a solution somewhat similar to what I know from the color selection for syntactic items in C/C++ code in Eclipse CDT's preferences which looks like this:
Eclipse color chooser.PNG
I created a subclass of QWizardPage which contains (among other widgets) a QLabel which contains the text "Color:" followed by a QPushButton:
Qt widgets.PNG
With this code placed in the wizard page's ctor. I implement the color
chooser functionality:
VisualPropertiesPage
::VisualPropertiesPage(QWidget *parent
) : QWizardPage(parent),
ui(new Ui::VisualPropertiesPage) {
ui->setupUi(this);
// Initialize the color chooser.
pal.
setColor(QPalette::Button, Qt
::black);
pal.
setColor(QPalette::ButtonText, Qt
::white);
ui->lineColorBtn->setPalette(pal);
[...]
VisualPropertiesPage::VisualPropertiesPage(QWidget *parent) :
QWizardPage(parent),
ui(new Ui::VisualPropertiesPage) {
ui->setupUi(this);
// Initialize the color chooser.
QPalette pal;
pal.setColor(QPalette::Button, Qt::black);
pal.setColor(QPalette::ButtonText, Qt::white);
ui->lineColorBtn->setPalette(pal);
[...]
To copy to clipboard, switch view to plain text mode
To execute the QColorDialog I implement a slot method as follows:
void VisualPropertiesPage::on_lineColorBtn_clicked() {
ui->lineColorBtn->setPalette(pal);
}
void VisualPropertiesPage::on_lineColorBtn_clicked() {
QColor color = QColorDialog::getColor(Qt::black, this, "Pick a color",
QColorDialog::DontUseNativeDialog);
QPalette pal;
pal.setColor(QPalette::Button, color);
ui->lineColorBtn->setPalette(pal);
}
To copy to clipboard, switch view to plain text mode
This way I can easily select a color. My problem now is:
How can I access the choosen color in the wizard's accept() method?
I know that there is the method
registerField(cons QString &name, QWidget *widget, const char *property, const char *changedSignal)
but couldn't figure out yet, how to call it to gain access to the
button's color. I don't know how to specify the third and fourth parameter.
Or is there possibly a completely different (and easier) way to
configure a color value in a wizard?
Any hint is appreciated!
Bookmarks