Results 1 to 5 of 5

Thread: How can I configure a color in a wizard?

  1. #1
    Join Date
    Oct 2017
    Posts
    18
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default How can I configure a color in a wizard?

    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:

    Qt Code:
    1. VisualPropertiesPage::VisualPropertiesPage(QWidget *parent) :
    2. QWizardPage(parent),
    3. ui(new Ui::VisualPropertiesPage) {
    4.  
    5. ui->setupUi(this);
    6.  
    7.  
    8. // Initialize the color chooser.
    9. QPalette pal;
    10. pal.setColor(QPalette::Button, Qt::black);
    11. pal.setColor(QPalette::ButtonText, Qt::white);
    12. ui->lineColorBtn->setPalette(pal);
    13.  
    14. [...]
    To copy to clipboard, switch view to plain text mode 

    To execute the QColorDialog I implement a slot method as follows:


    Qt Code:
    1. void VisualPropertiesPage::on_lineColorBtn_clicked() {
    2. QColor color = QColorDialog::getColor(Qt::black, this, "Pick a color",
    3. QColorDialog::DontUseNativeDialog);
    4. QPalette pal;
    5. pal.setColor(QPalette::Button, color);
    6. ui->lineColorBtn->setPalette(pal);
    7. }
    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!

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,229
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How can I configure a color in a wizard?

    By far the easiest method is to simply declare a QColor member variable in your wizard page class and set it to the user's choice instead of retrieving the color into a temporary variable in the slot. You can then implement a "getter" method to retrieve the color in the accept handler.

    Or, to be really in the Qt mindset, your VisualPropertiesPage should emit a signal ("lineColorChanged( const QColor & )" say) with the new value of the color. The wizard implements a slot to handle that signal and store it away in the user preferences somewhere. This decouples the wizard from the wizard pages; all the wizard needs to know is that the page will emit a signal when the color changes. It doesn't have to know anything else about the page.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Oct 2017
    Posts
    18
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: How can I configure a color in a wizard?

    Hi,

    thanks a lot for your helpful instructions! I first tried out the Qt-ish solution based on signals and slots as follows:

    In the wizard page class I added a connect(...) statement:

    Qt Code:
    1. VisualPropertiesPage::VisualPropertiesPage(QWidget *parent) :
    2. QWizardPage(parent),
    3. ui(new Ui::VisualPropertiesPage) {
    4.  
    5. ui->setupUi(this);
    6.  
    7. [...]
    8. GraphWizard *wiz = static_cast<GraphWizard*>(wizard());
    9. connect(this, SIGNAL(lineColorSet(const QColor&)), wiz, SLOT(setLineColor(const QColor&)));
    10. [...]
    To copy to clipboard, switch view to plain text mode 

    I defined a signal lineColorSet(QColor &color):

    Qt Code:
    1. class VisualPropertiesPage : public QWizardPage {
    2. Q_OBJECT
    3.  
    4. [...]
    5. signals:
    6. void lineColorSet(const QColor &color);
    7. [...]
    To copy to clipboard, switch view to plain text mode 

    Finally in the slot method on_lineColorBtn_clicked() I added an emit staetement:

    Qt Code:
    1. void VisualPropertiesPage::on_lineColorBtn_clicked() {
    2. QColor color = QColorDialog::getColor(Qt::black, this, "Pick a color",
    3. QColorDialog::DontUseNativeDialog);
    4. QPalette pal;
    5. pal.setColor(QPalette::Button, color);
    6. ui->lineColorBtn->setPalette(pal);
    7. QWizard *aWizard = wizard();
    8.  
    9. emit lineColorSet(color);
    10. }
    To copy to clipboard, switch view to plain text mode 

    In the QWizard subclass GraphWizard I defined a member

    Qt Code:
    1. QColor mLineColor;
    To copy to clipboard, switch view to plain text mode 

    and a slot to set that member:

    Qt Code:
    1. public slots:
    2. void setLineColor(const QColor &color);
    To copy to clipboard, switch view to plain text mode 

    Now the signal slot mechanism should work. Much to my surprise this didn't work. The signal is emitted but the setLineColor(...) method in GraphWizard does not get called. I have no idea why. So I implemented the needed functionality in a somewhat similar way to your first proposal:

    In the slot method on_lineColorBtn_clicked() instead of the emit statement I added this line:

    Qt Code:
    1. QWizard *aWizard = wizard();
    2. static_cast<GraphWizard*>(aWizard)->setLineColor(color);
    To copy to clipboard, switch view to plain text mode 

    This way the color is passed to the wizard and can be accessed there in the accept() method.

    While this now in principle works fine I'm still curious if there would be a possibility to implement this functionality somehow using the registerField(...)/field(...) mechanism. As already mentioned one normally would place a registerField(...) method in the ctor. of the wizard page. The method's signature looks as follows:

    Qt Code:
    1. void registerField(const QString &name, QWidget *widget, const char *property = Q_NULLPTR,
    2. const char *changedSignal = Q_NULLPTR)
    To copy to clipboard, switch view to plain text mode 


    So my idea now is to pass the palette object through the field. So my call to registerField should look somewhat similar to this:

    Qt Code:
    1. registerField("lineColorPalette", ui->lineColorBtn, ui->lineColorBtn->palette(),
    2. ui->lineColorBtn-><SomeSignal>);
    To copy to clipboard, switch view to plain text mode 


    Any ideas?

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,229
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How can I configure a color in a wizard?

    GraphWizard *wiz = static_cast<GraphWizard*>(wizard());
    connect(this, SIGNAL(lineColorSet(const QColor&)), wiz, SLOT(setLineColor(const QColor&)));
    I would do this the other way around: make the connection in the wizard where you create the property page, not in the property page constructor.

    Personally, I think the registerField() method is not a very good idea. It tightly couples a change that the user is making to some property value to the detailed UI implementation of the widgets used to make the change. Suppose you discover some fabulous "ColorChooserButton" widget that works perfectly instead of what you are using now. In addition to replacing the button in your UI, you'll also probably have to change the registerField() method since this new button probably won't have the same properties as a standard pushbutton, and your wizard code will have to change too.

    With the signal / slot approach, it doesn't matter if you use a pushbutton, a fancy ColorChooserButton, or your mother to implement setting the property. All the wizard needs to know is that however the color gets changed, it will get a signal when it does.

    I don't know why your slot doesn't get called. You'd have to show more than small pieces of code so we can follow the logic and see how and where you are making the connection. If the connect() call doesn't work, Qt should be issuing a warning on the debug output that says so.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  5. #5
    Join Date
    Oct 2017
    Posts
    18
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: How can I configure a color in a wizard?

    Hi,

    thanks a lot for your valuable hint! Think, I now understand your design that decouples the way the color is determined on the QWizardPage subclass VisualPropertiesPage from the use of the value in the QWizard subclass GraphWizard's accept() method. So I added this simple connect statement in the GraphWizard's constructor (must admit that in between I renamed lineColor to penColor:-):

    Qt Code:
    1. connect(ui->visualPropertiesPage, SIGNAL(penColorSet(QColor)), this, SLOT(setPenColor(QColor)));
    To copy to clipboard, switch view to plain text mode 
    and in the VisualProperties class's slot method on_penColorBtn_clicked I added an emit statement that emits the

    Qt Code:
    1. penColorSet(QColor &color)
    To copy to clipboard, switch view to plain text mode 
    signal. All this works now like a charm and I gained some better understanding of good design with Qt!

  6. The following user says thank you to apatwork for this useful post:

    d_stranz (10th November 2017)

Similar Threads

  1. Replies: 6
    Last Post: 27th May 2014, 09:20
  2. using wizard
    By arjita in forum Newbie
    Replies: 14
    Last Post: 28th July 2012, 19:48
  3. Replies: 1
    Last Post: 29th September 2011, 12:14
  4. To Wizard or Not ?
    By fassage in forum Qt Programming
    Replies: 1
    Last Post: 6th November 2009, 11:42
  5. How to add a Wizard ?
    By npc in forum Qt Tools
    Replies: 4
    Last Post: 24th July 2007, 15:28

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.