4

I have subclassed QDialog and I have created a const method, because I want to definitely prevent modifications of my instance. Now if a certain error occurs, I would like to use a QMessageBox to display it. But I can't use this as the parent of the message box, because this is const.

This is a pity. According to the documentation (https://doc.qt.io/qt-5/qdialog.html#QDialog) the parent influences the default location of the new dialog and whether it shares the parent's taskbar entry. Does the parent necessarily have to be non-const for that...?

I see three options, none of them being obviously excellent:

  • const_cast (seems strange to me to use const_cast in such a common situation)
  • use nullptr as parent (ugly, because the message box position is worse)
  • make my method non-const (ugly, because the compiler would not anymore support me in protecting the instance)

Is Qt not const-correct when it demands the parent widget to be modifiable? And is there a better solution than the const_cast?

Benjamin Bihler
  • 1,612
  • 11
  • 32

1 Answers1

1

One of the reason to introduce ..._cast methods is to differntiate data castind and same time to protect from unwanted casting and make code more readable and easy search in code.

Operator const_cast is exactly that thing for your taks. The mark const in method mean that your operations will not modify any object state data. However make alert dialog or any window require modify much states in operation system and application internal data. That is the reason why it is not const opearations.

However apper and close alert dialog is not change any state in data of your object and not concern your task. So in point o view os and application state management create dialog is not const. But in ponit of view of task which you solve in your application the apearance of alert dialog is a const operation.

Therefore const_cast is exactly that what you need to make join of different tasks where them is intersected.

oklas
  • 7,935
  • 2
  • 26
  • 42
  • Thank you for that answer. I get your point. Still I wonder why modifying "states in operation system and application internal data" means that the parent widget may not be `const`...?! – Benjamin Bihler Jul 11 '19 at 12:29
  • 2
    This is interface of object designed by creators. Actually it means that object passed to function (or constructor) will be used in non const manner. So you (and compiler) will know that. Some times interface methos require const references and call non const methos with const cast but it is internal and from point of view of user that is const. – oklas Jul 11 '19 at 12:34
  • Specifically parent window may not be const at least because it manage children windows and have a list of children windows and this list wil be modified (adition/removing) with new child and so on – oklas Jul 11 '19 at 12:48
  • I have asked another question, because I have doubts whether `const_cast` could then even lead to undefined behaviour: https://stackoverflow.com/questions/56989756/is-const-castthis-with-a-write-operation-undefined-behaviour-if-the-actual-ob. – Benjamin Bihler Jul 11 '19 at 12:56
  • Okay, according to the answers to my other question everything seems to be safe. Thank you again for your answer. Still I am not fully content with Qt here, since my code is not littered with `const_cast`s. ;) – Benjamin Bihler Jul 11 '19 at 13:11