Reputation: 2609
I'm trying to make a simple Qt program.I have successfully build and run the program several times.These errors occurred when I added a getter method to pass a string from my MainWindow to a dialog's QFileDialog::getSaveFileName()
But when I comment below lines the programs runs successfully.
MainWindow.h
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
QString fileName();
};
MainWindow.cpp
QString MainWindow::fileName() {
return "F:/Users/Admin/Desktop/" + dnldName;
}
Usage of fileName()
void Dialog::on_browseButton_clicked()
{
QFileDialog folder;
folder.setFileMode(QFileDialog::Directory);
folder.setOption(QFileDialog::ShowDirsOnly);
dirPath = folder.getSaveFileName(this, tr("Save File"), mWinObj.fileName(), tr("All Files"));
ui->savePathEdit->setText(dirPath);
}
The program builds successfully but gives the following errors when I try to run it.
Starting F:\Users\Admin\Desktop\Imp Docs\C++ apps\build-GUINetworkApp-Desktop_Qt_5_2_0_MinGW_32bit-Debug\debug\GUINetworkApp.exe...
QWidget: Must construct a QApplication before a QWidget
Invalid parameter passed to C runtime function.
Invalid parameter passed to C runtime function.
F:\Users\Admin\Desktop\Imp Docs\C++ apps\build-GUINetworkApp-Desktop_Qt_5_2_0_MinGW_32bit-Debug\debug\GUINetworkApp.exe exited with code 3
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
What's wrong with the code and how can I fix it?
Upvotes: 6
Views: 21529
Reputation: 618
I know it's an old thread, but the solution is so unexpected, that every Google search hit should have this solution: This problem also occurs, at least on Windows, if you run a Debug build of your application with a Release build of vtk. (I encountered this before, and now did so again on Vtk 9.3.0 and Qt 6.6.2.). The solution for this -- having Vtk-targets that support both Debug and Release libraries -- is currently not trivial, since vtk does not support it direclty:
find_packag(Vtk...)
call with the necessary debug *d.dll
- and *d.lib
-files, similar to this: find_package(VTK COMPONENTS CommonCore)
get_target_property(Dll_CommonCore_Release VTK::CommonCore IMPORTED_LOCATION_RELEASE)
get_target_property(Lib_CommonCore_Release VTK::CommonCore IMPORTED_IMPLIB_RELEASE)
string(REPLACE ".dll" "d.dll" Dll_CommonCore_Debug ${Dll_CommonCore_Release})
string(REPLACE ".lib" "d.lib" Lib_CommonCore_Debug ${Lib_CommonCore_Release})
set_target_properties(VTK::CommonCore PROPERTIES
IMPORTED_LOCATION_DEBUG ${Dll_CommonCore_Debug}
IMPORTED_IMPLIB_DEBUG ${Lib_CommonCore_Debug}
)
Upvotes: 1
Reputation: 1349
You cannot create widgets as global objects since those objects will be created before the application object in the main function. Then your error will happen.
Create your widget within the main function after the QApplication object construction, or just keep a global pointer to your widget if you want global access (but which is not a good programming style).
Upvotes: 8