Jump to content

Unresolved external error when using MessageBox() in Qt Creator (also how to statically link Qt project?)

ClobberXD

I've included Windows.h for the MessageBox() function, and yet it throws an unresolved external error! Manually link even the Windows lib? I don't even know the name of it! VS automatically links those libraries...

 

And also, how do I statically link projects using Qt Creator?

 

Thanks!

Nothing to see here ;)

Link to comment
Share on other sites

Link to post
Share on other sites

9 hours ago, Unimportant said:

You should use the QMessageBox class in QT.

 

As for the static QT build: https://wiki.qt.io/Build_Standalone_Qt_Application_for_Windows

Do note that building the QT static lib takes a long long time.

Thanks for QMessageBox - it works perfectly. But how tedious it is, to static link a program!

Nothing to see here ;)

Link to comment
Share on other sites

Link to post
Share on other sites

18 hours ago, Unimportant said:

You should use the QMessageBox class in QT.

 

As for the static QT build: https://wiki.qt.io/Build_Standalone_Qt_Application_for_Windows

Do note that building the QT static lib takes a long long time.

Can you also help me configure the project as 32-bit? Thanks!

Nothing to see here ;)

Link to comment
Share on other sites

Link to post
Share on other sites

I've never actually built the Qt libraries from source as static. Technically, statically linking the Qt libraries is against the license (GNU GPL), but if you aren't distrubuting the code for profit, you should be fine.

With all that said, if you are compiling it on windows, just make sure to use the specified build tools (mingw32-make, msvc32-nmake). If you plan on building with mingw, then nothing addition is needed, as there does not exist a mingw64 at the moment. If you are building with MSVC, you need to specify that build target before building. From that static build link, it says

> configure -static -release -platform win32-msvc
> nmake sub-src

so just make to include the -platform win32-msvc when configuring, and you'll be all set.

And as @Unimportant said, use the QMessageBox class. All Qt GUI stuff is prefixed with a Q. So they have things like QMainWindow, QMessageBox, QPushButton, etc. I am pretty familiar with Qt so if you run into any problems with it, post here and I should be able to help. Qt has a lot of little things that you'll probably run into, but overall it is awesome.

Link to comment
Share on other sites

Link to post
Share on other sites

23 minutes ago, Pinguinsan said:

I've never actually built the Qt libraries from source as static. Technically, statically linking the Qt libraries is against the license (GNU GPL), but if you aren't distrubuting the code for profit, you should be fine.

With all that said, if you are compiling it on windows, just make sure to use the specified build tools (mingw32-make, msvc32-nmake). If you plan on building with mingw, then nothing addition is needed, as there does not exist a mingw64 at the moment. If you are building with MSVC, you need to specify that build target before building. From that static build link, it says


> configure -static -release -platform win32-msvc
> nmake sub-src

so just make to include the -platform win32-msvc when configuring, and you'll be all set.

And as @Unimportant said, use the QMessageBox class. All Qt GUI stuff is prefixed with a Q. So they have things like QMainWindow, QMessageBox, QPushButton, etc. I am pretty familiar with Qt so if you run into any problems with it, post here and I should be able to help. Qt has a lot of little things that you'll probably run into, but overall it is awesome.

Where do I include these config instructions? I've no idea! Thanks!

Nothing to see here ;)

Link to comment
Share on other sites

Link to post
Share on other sites

11 hours ago, Pinguinsan said:

I've never actually built the Qt libraries from source as static. Technically, statically linking the Qt libraries is against the license (GNU GPL), but if you aren't distrubuting the code for profit, you should be fine.

With all that said, if you are compiling it on windows, just make sure to use the specified build tools (mingw32-make, msvc32-nmake). If you plan on building with mingw, then nothing addition is needed, as there does not exist a mingw64 at the moment. If you are building with MSVC, you need to specify that build target before building. From that static build link, it says


> configure -static -release -platform win32-msvc
> nmake sub-src

so just make to include the -platform win32-msvc when configuring, and you'll be all set.

And as @Unimportant said, use the QMessageBox class. All Qt GUI stuff is prefixed with a Q. So they have things like QMainWindow, QMessageBox, QPushButton, etc. I am pretty familiar with Qt so if you run into any problems with it, post here and I should be able to help. Qt has a lot of little things that you'll probably run into, but overall it is awesome.

Thanks to @Unimportant and you :), I've created a QComboBox for my application using the Qt Designer. But how do I get the current index of the comboBox? When I use the object name it says "undeclared identifier". But it's the exact name created by Qt Designer... How do I even reference GUI elements created using Qt Designer?

 

Thanks!

Nothing to see here ;)

Link to comment
Share on other sites

Link to post
Share on other sites

If you generated the project with Qt Creator, it will have automatically created a MainWindow class. In the mainwindow.h, you should have a private member called ui. In the mainwindow.cpp file, you'll have a MainWindow constructor that has an initializer for the ui member, something like

 

 MainWindow::MainWindow(QWidget *parent=0) : ui(new Ui:: MainWindow) 

 

and in the body, you'll see

 ui->setupUi(this); 

The ui object contains all of the member from your mainwindow.ui designer form. So for example, if you have a QPushButton on the form called pushButton, and you wanted to set it to disabled, you'd access it by saying

 

 ui->pushButton->setEnabled(false);

 

Just make sure that you don't try to access them before the ui->setupUi() function is called in the constructor, or the program will crash (dereferencing a null pointer).

 

EDIT: For info relating specifically to a QComboBox, see here

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, Pinguinsan said:

If you generated the project with Qt Creator, it will have automatically created a MainWindow class. In the mainwindow.h, you should have a private member called ui. In the mainwindow.cpp file, you'll have a MainWindow constructor that has an initializer for the ui member, something like

 


 MainWindow::MainWindow(QWidget *parent=0) : ui(new Ui:: MainWindow) 

 

and in the body, you'll see


 ui->setupUi(this); 

The ui object contains all of the member from your mainwindow.ui designer form. So for example, if you have a QPushButton on the form called pushButton, and you wanted to set it to disabled, you'd access it by saying

 


 ui->pushButton->setEnabled(false);

 

Just make sure that you don't try to access them before the ui->setupUi() function is called in the constructor, or the program will crash (dereferencing a null pointer).

 

EDIT: For info relating specifically to a QComboBox, see here

Does everything above apply to widgets? I've selected QWidget as the base class, so widget.h, widget.cpp, etc...

Nothing to see here ;)

Link to comment
Share on other sites

Link to post
Share on other sites

8 hours ago, Anand_Geforce said:

Does everything above apply to widgets? I've selected QWidget as the base class, so widget.h, widget.cpp, etc...

Yep! Your main files will just be widget.h and widget.cpp, and the constructor will be for Widget rather than MainWindow, but the ui object is still valid.

Link to comment
Share on other sites

Link to post
Share on other sites

All right! No errors are thrown up! But I'd like to output the ui->comboBox->currentIndex() through QMessageBox, and I'm not able to achieve that using QMessageBox::setText(const QString &); Can you help me? And also:

On 8/15/2016 at 7:56 PM, Pinguinsan said:

 


> configure -static -release -platform win32-msvc
> nmake sub-src

 

Where do I put this? :S

 

BTW, I've installed this: qt-opensource-windows-x86-msvc2015_64-5.7.0.exe

Isn't this 'x86'? It has auto-detected MSVC_x64 or something (64-bit compiler) and uses that only! I've manually added MSVC_x86 or something as MSVC 32-bit compiler, but when I try to use it, an error pops up :

Quote

build-Sample-Desktop_Qt_5_7_0_MSVC2015_64bit-Release\release\widget.obj:-1: error: LNK1112: module machine type 'X86' conflicts with target machine type 'x64'

Any idea why?

 

Thanks! :)

Nothing to see here ;)

Link to comment
Share on other sites

Link to post
Share on other sites

15 hours ago, Anand_Geforce said:

All right! No errors are thrown up! But I'd like to output the ui->comboBox->currentIndex() through QMessageBox, and I'm not able to achieve that using QMessageBox::setText(const QString &); Can you help me?

Qt uses QString for all it's string requirements, best to familiarize yourself with QString.

 

   QMessageBox MessageBox;

    MessageBox.setWindowTitle("My message box!");
    MessageBox.setText(QString("Current index is:") + QString::number(ui->comboBox->currentIndex())); 
    MessageBox.exec();

QString::number is a static memberfunction which can create a new QString from a number. The new string is then appended to the other QString because QString overrides "operator +".

Link to comment
Share on other sites

Link to post
Share on other sites

On 8/16/2016 at 9:34 PM, Anand_Geforce said:

All right! No errors are thrown up! But I'd like to output the ui->comboBox->currentIndex() through QMessageBox, and I'm not able to achieve that using QMessageBox::setText(const QString &); Can you help me? And also:

Where do I put this? :S

 

BTW, I've installed this: qt-opensource-windows-x86-msvc2015_64-5.7.0.exe

Isn't this 'x86'? It has auto-detected MSVC_x64 or something (64-bit compiler) and uses that only! I've manually added MSVC_x86 or something as MSVC 32-bit compiler, but when I try to use it, an error pops up :

Any idea why?

 

Thanks! :)

When you install the qt-opensource-windows...exe, you're installing Qt binaries that have already been built, using that specific compiler (MSVC x86_64, in that case). Then, Qt Creator automatically detected that you have that compiler (cl.exe, I think), and it automatically set up that "kit" for you. So basically, just because you install the prebuilt Qt binaries for a certain compiler, doesn't mean you can compile your application (because you may not have the compiler). Confusing, huh? As such, there's no way to use that x86_64 binary as a 32 bit compiler. However, since you have x86_64 MSVC compiler, you most likely have the x86 MSVC compiler too. To add the prebuilt binaries for MSVC x86, search your computer for "Uninstall Qt" and execute the shortcut it brings up. When it asks you what you want to do, select "Add Or Remove Components". It'll think for a moment and grab some stuff from a remote repository, then you'll need to expand whichever Qt version you currently have installed (it will have it's checkbox filled in next to the one you have, probably Qt 5.7). Then, it should have at least one of the binaries already selected (in your case, msvc2015 64-bit). Whichever year is selected, select the 32-bit version of it (so if you have msvc2015 64-bit selected, pick the msvc2015 32-bit), and hit next to install it. Now, when you open up Qt Creator, it should automatically create the 32-bit MSVC kit for you, which you can select when you go to make a new project.

Now the above is all separate from the "build Qt as static" issue. The stuff above is all dynamically linked by default. To build the libraries as static, you need to first clone the sources using git (from the command line):
 

git clone https://code.qt.io/qt/qt5.git

  Then, go into the directory and checkout the qt5 branch

cd qt5
git checkout 5.7

From there, it gets a lot more complicated (especially for windows), but you basically have to follow along here, and when you get the the "configure" step, substitute the configure and nmake lines listed above for whatever that page lists. Sorry I can't instruct you further on the building portion, but it would honestly just be me typing out what shows up on that page, because I've never done it before either :ph34r:

Link to comment
Share on other sites

Link to post
Share on other sites

3 hours ago, Pinguinsan said:

When you install the qt-opensource-windows...exe, you're installing Qt binaries that have already been built, using that specific compiler (MSVC x86_64, in that case). Then, Qt Creator automatically detected that you have that compiler (cl.exe, I think), and it automatically set up that "kit" for you. So basically, just because you install the prebuilt Qt binaries for a certain compiler, doesn't mean you can compile your application (because you may not have the compiler). Confusing, huh? As such, there's no way to use that x86_64 binary as a 32 bit compiler. However, since you have x86_64 MSVC compiler, you most likely have the x86 MSVC compiler too. To add the prebuilt binaries for MSVC x86, search your computer for "Uninstall Qt" and execute the shortcut it brings up. When it asks you what you want to do, select "Add Or Remove Components". It'll think for a moment and grab some stuff from a remote repository, then you'll need to expand whichever Qt version you currently have installed (it will have it's checkbox filled in next to the one you have, probably Qt 5.7). Then, it should have at least one of the binaries already selected (in your case, msvc2015 64-bit). Whichever year is selected, select the 32-bit version of it (so if you have msvc2015 64-bit selected, pick the msvc2015 32-bit), and hit next to install it. Now, when you open up Qt Creator, it should automatically create the 32-bit MSVC kit for you, which you can select when you go to make a new project.

Now the above is all separate from the "build Qt as static" issue. The stuff above is all dynamically linked by default. To build the libraries as static, you need to first clone the sources using git (from the command line):
 


git clone https://code.qt.io/qt/qt5.git

  Then, go into the directory and checkout the qt5 branch


cd qt5
git checkout 5.7

From there, it gets a lot more complicated (especially for windows), but you basically have to follow along here, and when you get the the "configure" step, substitute the configure and nmake lines listed above for whatever that page lists. Sorry I can't instruct you further on the building portion, but it would honestly just be me typing out what shows up on that page, because I've never done it before either :ph34r:

Thanks for the 32-bit part! I've successfully set up Qt 5.7.0 msvc_x86. But the static-linking part is too tedious O.o! I'll stick with dynamic for now... But how do I know what DLLs to place with the .exe file? Also DLLs have multiple versions...

Nothing to see here ;)

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×