Peter
Peter

Reputation: 11920

How to cancel out of QColorDialog::getColor()?

In a QT application I am working on, we let the user pick a color using QColorDialog::getColor(). Based on an external event, I need to cancel this opened dialog. Is there a way to do it? I didn't see any other static method on QColorDialog to exit out of the dialog.

Or, may be a better method would be to close all opened dialogs. Is there such a method?

Upvotes: 2

Views: 4444

Answers (4)

tatsy
tatsy

Reputation: 324

Following this Qt Forum post,

http://www.qtforum.org/article/37032/ok-cancel-buttons-on-qcolordialog.html

I tested the following code.

QColor color = QColorDialog::getColor();
if (!color.isValid()) return;

// Your process for selected color
// ...

and it properly worked for me.

Upvotes: 5

Kyef
Kyef

Reputation: 79

This works for me:

QColorDialog *dialog = new QColorDialog(this);
dialog->show();
QObject::connect(dialog,&QDialog::accepted,[=](){
    QColor color =  dialog->currentColor();
    QVariant variant = color;
    QString rgb= variant.toString();
    ui->eg->setStyleSheet("QLabel { color :"+rgb+" ; }");});`

I hope It helps someone! The above works to change the QLabel font and/or frame but u can try different stylesheets i.e

ui->label->setStyleSheet("QLabel { background-color :"+rgb+" ; color : white; }");

Upvotes: 1

Ezee
Ezee

Reputation: 4344

Here is the code which you call by QColorDialog::getColor:

QColorDialog dlg(parent);
if (!title.isEmpty())
    dlg.setWindowTitle(title);
dlg.setOptions(options);
dlg.setCurrentColor(initial);
dlg.exec();
return dlg.selectedColor();

As you can see it creates an instance of QColorDialog of stack, sets its initial properties, shows it and returns the result. You can use the same code to create the dialog BUT pay attention on how the dialog is shown.
Method QDialog::exec creates a new event loop (http://qt-project.org/doc/qt-4.8/qeventloop.html) and don't return until the dialog is closed.
That's why you can't call any method of QDialog. Thus QDialog::exec creates so called modal window (http://qt-project.org/doc/qt-4.8/qwidget.html#windowModality-prop).

Solution
To be able to interact with the dialog you need to create it using operator new and use method QDialog::show to show the dialog. But this method returns control immediately when the dialog is shown. So you won't be able to get the color in the next line of your code. Instead you need to subscribe to the dialog signals accepted and rejected, process the results (dialog->currentColor()) and delete the dialog object.

Also you've asked about a way to close all opened dialog. Supposing that all you dialogs are inherited from QDialog:

foreach (QWidget *widget, QApplication::topLevelWidgets()) {
     if (QDialog* dialog = qobject_cast<QDialog*>(widget))
         dialog->close();
 }

Upvotes: 2

Silicomancer
Silicomancer

Reputation: 9186

You can not do this when using the static getColor() function.

Construct a dialog object instead so you get a pointer allowing you to call all available functions (like reject() or close()).

Upvotes: 0

Related Questions