2013-07-21 42 views
2
QMessageBox::warning(this,tr("Error"), 
          tr("File existed")); 

I use QtCreator for MSVS2012,Win7. "this" points to a class that public inherited from QWizard class, the compiler output isAbout Qt MessageBox::warning() overloading

error C2665: 「QMessageBox::warning」: 4 個重載中沒有一個可以轉換所有參數類型 d:\qt\qt5.1.0\5.1.0\msvc2012_64\include\qtwidgets\qmessagebox.h(197): 可能是「QMessageBox::StandardButton QMessageBox::warning(QWidget *,const QString & ,const QString & ,QMessageBox::StandardButtons,QMessageBox::StandardButton)」 嘗試匹配參數列表「(const newWizard *const , QString, QString)」時

it means none of the 4 overloads could convert all the argument types. Anyone can give me some help?

+0

Just to get this clear, is the '個重載中沒有一個可以轉換所有參數類型' a custom error message you created yourself in the source? Or (showing my total ignorance about Qt here) are you using a localised version of Qt? –

+0

@MrLister: he's using a localized version of Windows it seems. –

+2

It looks like 'this' is a pointer to a const QWidget, while it requires a non-const object. – JRG

回答

1

Replace this with 0 and it should work.

Basically, the dialog box doesn't need a parent. It can stand alone and not have a problem.

https://qt-project.org/doc/qt-4.8/objecttrees.html

And like the comments to your question said, you can't call the warning in a const method either.

Another option is that you could get rid of the const 'ness of your newWizard() method.

Hope that helps.

+0

I thought the dialog box must have a parent!!Thank you. – Jiang42

+0

@Marz: Technically dialog must not have a parent, but its good practice to give every window one, as not assigning parent can cause issues with window stacking (modal message boxes coming up in the background behind the disabled main window; nasty!). That's especially evil on OS X. –

1

Try to cast this to QWidget * And I agree with @phyatt, you can set parent = 0. Mean that your warning has no parent and it's child of desktop.

E.g:

QMessageBox::warning((QWidget *)this,tr("Error"), 
          tr("File existed")); 
QMessageBox::warning(0,tr("Error"), 
          tr("File existed")); 
+0

Your advice is also useful.In fact I use const_cast to solve this problem. – Jiang42

+0

Yup, const_cast is better :) – trandatnh