Return QMessageBox::Icon instead of bool to check if method worked fine
-
Hi,
My question is about preventing errors that users can make.
Usually we write:QDir dir("example"); if (!dir.exists()) { QMessageBox::critical(nullptr, "Error", "Cannot find the example directory"); return false; }
But is it ok to return QMessageBox::Icon like in the following code? Or maybe nobody does like this and this is bad form?
QDir dir("example"); if (!dir.exists()) { QMessageBox::critical(nullptr, "Error", "Cannot find the example directory"); return QMessageBox::Icon::Critical; }
-
@JonB thank you for commenting
I'm in doubt, I don't know exactly what I want.
bool has two states and QMessageBox::Icon has few of them. Probably in future I will process not onlyQMessageBox::Icon::Critical
but alsoQMessageBox::Icon::Warning
I don't know
But I feel it is a strange output...I don't have enough experience to cath errors so I'm experimenting
@Please_Help_me_D
Hi
Why not defines your own enum and return one of these to tell the caller what happened.
So it matches what you are really doing.enum FileSelectStatus{ ExampleDirNotFound , DirIsEmpty, InvalidX, AllOK };
Or what else can go wrong.
-
Hi,
My question is about preventing errors that users can make.
Usually we write:QDir dir("example"); if (!dir.exists()) { QMessageBox::critical(nullptr, "Error", "Cannot find the example directory"); return false; }
But is it ok to return QMessageBox::Icon like in the following code? Or maybe nobody does like this and this is bad form?
QDir dir("example"); if (!dir.exists()) { QMessageBox::critical(nullptr, "Error", "Cannot find the example directory"); return QMessageBox::Icon::Critical; }
@Please_Help_me_D said in Return QMessageBox::Icon instead of bool to check if method worked fine:
But is it ok to return QMessageBox::Icon like in the following code?
It's a free world (just). Yes, you can do that. Depends on the function's return type.
Or maybe nobody does like this and this is bad form?
It would be a very odd choice for function return type;
bool false
would be usual. Why would you want to return this? -
@Please_Help_me_D said in Return QMessageBox::Icon instead of bool to check if method worked fine:
But is it ok to return QMessageBox::Icon like in the following code?
It's a free world (just). Yes, you can do that. Depends on the function's return type.
Or maybe nobody does like this and this is bad form?
It would be a very odd choice for function return type;
bool false
would be usual. Why would you want to return this?@JonB thank you for commenting
I'm in doubt, I don't know exactly what I want.
bool has two states and QMessageBox::Icon has few of them. Probably in future I will process not onlyQMessageBox::Icon::Critical
but alsoQMessageBox::Icon::Warning
I don't know
But I feel it is a strange output...I don't have enough experience to cath errors so I'm experimenting
-
@JonB thank you for commenting
I'm in doubt, I don't know exactly what I want.
bool has two states and QMessageBox::Icon has few of them. Probably in future I will process not onlyQMessageBox::Icon::Critical
but alsoQMessageBox::Icon::Warning
I don't know
But I feel it is a strange output...I don't have enough experience to cath errors so I'm experimenting
@Please_Help_me_D
Hi
Why not defines your own enum and return one of these to tell the caller what happened.
So it matches what you are really doing.enum FileSelectStatus{ ExampleDirNotFound , DirIsEmpty, InvalidX, AllOK };
Or what else can go wrong.
-
@Please_Help_me_D
Hi
Why not defines your own enum and return one of these to tell the caller what happened.
So it matches what you are really doing.enum FileSelectStatus{ ExampleDirNotFound , DirIsEmpty, InvalidX, AllOK };
Or what else can go wrong.
@mrjj thank you
Maybe I will create something likeQMessageBox
(or inherit from it) with the opportunity to retrieve enum from appeared window (warning or critical or something else) -
@mrjj thank you
Maybe I will create something likeQMessageBox
(or inherit from it) with the opportunity to retrieve enum from appeared window (warning or critical or something else)@Please_Help_me_D
Hi
It would be easier just to put in a function that returns your enum and then use that instead of
QMessageBox directly.