Qt for begginers :10 subclassing Qt widget .
-
Hello , I have a problem . It can not link .
main.obj:-1: ошибка: LNK2019: unresolved external symbol "public: __cdecl Window::Window(class QWidget *)" (??0Window@@QEAA@PEAVQWidget@@@Z) referenced in function main
Can some one help me ? -
Hi, welcome to devnet.
Apparently you have not defined a constructor for your class Window.
-
I copy and insert text in my project , I cant understand why this dont work =\
#include <QApplication>
#include "window.h"int main(int argc, char **argv)
{
QApplication app (argc, argv);Window window;
window.show();return app.exec();
} -
As I said - apparently you haven't defined a constructor for your Window class. Can you show Window.h and Window .cpp files?
-
//window.h
#ifndef WINDOW_H
#define WINDOW_H#include <QWidget>
class QPushButton;
class Window : public QWidget
{
public:
explicit Window(QWidget *parent = 0);
private:
QPushButton *m_button;
};#endif // WINDOW_H
//window.cpp
#include "window.h"#include <QPushButton>
Window::Window(QWidget *parent) :
QWidget(parent)
{
// Set size of the window
setFixedSize(100, 50);// Create and position the button
m_button = new QPushButton("Hello World", this);
m_button->setGeometry(10, 10, 80, 30);
} -
Ok, these seem fine. So if the linker can't see the constructor then it means the compiler didn't compile the Window.cpp file. Can you confirm you added it in your project file (.pro) and re-run qmake?
Btw. When pasting code in the forum surround it with ``` (three backticks). This will format it for better reading.
-
I have : main.cpp , Qt1.pro , window.cpp and window.h files . I dont know re-run qmake , Im begginer .
-
The project file is Qt1.pro. This is where you define which files are part of your project. There should be a section in it looking like this:
HEADERS += window.h SOURCES += main.cpp \ window.cpp
The error you pasted can mean you're missing the window.cpp file from the SOURCES section.
After you add it there (if it's missing) re-run qmake (it's a tool that reads this .pro file and generates makefiles for your compiler). You do that by clicking "run qmake" in the "Build" menu in Qt Creator. -
Thank you , "Run Qmake" work =) I use only green triangle >.< Now all work .
-
Great. You only need to run qmake when you change the contents of the project file.