I'm new to Qt and am trying to figure out how to add a custom widget I've created to my main window.
The widget is pretty simple - I just used Qt Designer to create the default widget (I'm calling it Editor) and then added a button to it. Next I included the header of my custom widget in mainwindow.h and added a pointer to it in my MainWindow class. So far, so good. But when I try to create a new instance of Editor, I get the error:
mainwindow.obj:-1: error: LNK2019: unresolved external symbol "public: __cdecl Editor::Editor(class QWidget *)" (??0Editor##QEAA#PEAVQWidget###Z) referenced in function "public: __cdecl MainWindow::MainWindow(class QWidget *)" (??0MainWindow##QEAA#PEAVQWidget###Z)
What do I need to do to be able to link my code?
mainwindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "editor.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
Editor *editor;
};
#endif // MAINWINDOW_H
mainwindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
editor = new Editor(0);
}
MainWindow::~MainWindow()
{
delete ui;
delete editor;
}
editor.h:
#ifndef EDITOR_H
#define EDITOR_H
#include <QWidget>
namespace Ui {
class Editor;
}
class Editor : public QWidget
{
Q_OBJECT
public:
explicit Editor(QWidget *parent = 0);
~Editor();
private:
Ui::Editor *ui;
};
#endif // EDITOR_H
editor.cpp:
#include "editor.h"
#include "ui_editor.h"
Editor::Editor(QWidget *parent) :
QWidget(parent),
ui(new Ui::Editor)
{
ui->setupUi(this);
}
Editor::~Editor()
{
delete ui;
}
CustomWidgetTest.pro:
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = CustomWidgetTest
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp \
editor.cpp
HEADERS += mainwindow.h \
editor.h
FORMS += mainwindow.ui \
editor.ui
Related
Welcome, I have a question about Qt's windows operations. I have a simple app which contains 2 windows:
MainWindow - includes push button, can't be resizeable,
AdminWindow - includes label, can be resizeable.
When I click push button, it should open AdminWindow and hide MainWindow. I made the app and it seems to work but when I open the AdminWindow, the application icon which is located in windows's bottom bar is missing. How can I fix it?
Icon is showed when MainWindow is opened:
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "adminwindow.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_pushButtonOpenAdmin_clicked();
private:
Ui::MainWindow *ui;
AdminWindow *adminWindow;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
// hides MainWindow and show AdminWindow
void MainWindow::on_pushButtonOpenAdmin_clicked()
{
hide();
adminWindow = new AdminWindow(this);
adminWindow->show();
}
adminwindow.h
#ifndef ADMINWINDOW_H
#define ADMINWINDOW_H
#include <QMainWindow>
namespace Ui {
class AdminWindow;
}
class AdminWindow : public QMainWindow
{
Q_OBJECT
public:
explicit AdminWindow(QWidget *parent = 0);
~AdminWindow();
private:
Ui::AdminWindow *ui;
};
#endif // ADMINWINDOW_H
adminwindow.cpp
#include "adminwindow.h"
#include "ui_adminwindow.h"
AdminWindow::AdminWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::AdminWindow)
{
ui->setupUi(this);
}
AdminWindow::~AdminWindow()
{
delete ui;
}
I would like to make a simple QT mainwindow with the button to open a second window or dialog. I followed literally the step from the QT link "Using a Designer UI File in Your Application" and following the single inheritance example.
But QT gives 4 errors , which you will see a snapshot of below.
Now, what I did is I created a mainwindow in Qt designer, then I added a second form to the project , which will be the second dialog window when a button clicked. Because I created the form manually "mydialog.ui", I added class "mydialog.h and mydialog.cpp" and put the header of "ui-mydialog" in the source file "mydialog.cpp".
I' not sure what am I missing ?
Below is the code :
- mydialog.h
#ifndef MYDIALOG_H
#define MYDIALOG_H
#include<QWidget>
class mydialog ;
namespace Ui {
class mydialog;
}
class mydialog : public QWidget
{
Q_OBJECT
public:
explicit mydialog(QWidget *parent = 0);
virtual ~mydialog();
private :
Ui::mydialog *ui;
};
#endif // MYDIALOG_H
- mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QtCore/QtGlobal>
#include <QMainWindow>
QT_USE_NAMESPACE
QT_BEGIN_NAMESPACE
namespace Ui {
class MainWindow;
}
QT_END_NAMESPACE
class mydialog;
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_Start_clicked();
private:
Ui::MainWindow *ui;
mydialog *dialog1;
};
#endif // MAINWINDOW_H
- mydialog.cpp
#include"mydialog.h"
#include "ui_mydialog.h"
mydialog::mydialog(QWidget *parent) : QWidget(parent), ui(new Ui::mydialog)
{
ui->setupUi(this);
}
mydialog::~mydialog()
{
delete ui;
}
- mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include"mydialog.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
dialog1 = new mydialog ;
}
MainWindow::~MainWindow()
{
delete ui;
delete dialog1;
}
void MainWindow::on_Start_clicked()
{
}
- main.cpp
#include"mainwindow.h"
#include<QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
- The .pro file
#-------------------------------------------------
#
# Project created by QtCreator 2015-12-17T00:10:58
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = TestTool
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp \
mydialog.cpp
HEADERS += mainwindow.h \
mydialog.h
FORMS += mainwindow.ui \
mydialog.ui
RESOURCES += \
misc.qrc
- Qt compilation output error
Compilation error
The generated file Ui_mydialog.h is :
#ifndef UI_MYDIALOG_H
#define UI_MYDIALOG_H
#include <QtCore/QVariant>
#include <QtWidgets/QAction>
#include <QtWidgets/QApplication>
#include <QtWidgets/QButtonGroup>
#include <QtWidgets/QDialog>
#include <QtWidgets/QDialogButtonBox>
#include <QtWidgets/QHeaderView>
QT_BEGIN_NAMESPACE
class Ui_Dialog
{
public:
QDialogButtonBox *buttonBox;
void setupUi(QDialog *Dialog)
{
if (Dialog->objectName().isEmpty())
Dialog->setObjectName(QStringLiteral("Dialog"));
Dialog->resize(400, 300);
buttonBox = new QDialogButtonBox(Dialog);
buttonBox->setObjectName(QStringLiteral("buttonBox"));
buttonBox->setGeometry(QRect(30, 240, 341, 32));
buttonBox->setOrientation(Qt::Horizontal);
buttonBox->setStandardButtons(QDialogButtonBox::Cancel|QDialogButtonBox::Ok);
retranslateUi(Dialog);
QObject::connect(buttonBox, SIGNAL(accepted()), Dialog, SLOT(accept()));
QObject::connect(buttonBox, SIGNAL(rejected()), Dialog, SLOT(reject()));
QMetaObject::connectSlotsByName(Dialog);
} // setupUi
void retranslateUi(QDialog *Dialog)
{
Dialog->setWindowTitle(QApplication::translate("Dialog", "Dialog", 0));
} // retranslateUi
};
namespace Ui {
class Dialog: public Ui_Dialog {};
} // namespace Ui
QT_END_NAMESPACE
#endif // UI_MYDIALOG_H
This is because of the diferences between the names in your UI- und C++sourcecode files.
For example if in your UI-sourcecode file you have a name like "StatusBarPart"
but the name of your class in the C++ file is "StatusBar"
<class>StatusBarPart</class>
<widget class="QWidget" name="StatusBarPart">
StatusBar::StatusBar(QWidget *parent)
: PartBase(parent),
ui(new Ui::StatusBar)
then you you get these error message you see.
Solution:
You can edit the UI file in some external editor and make the names equal. Save changes. Compile your app. Be happy ;-)
You are mixing the name of the ui file with the name of the Ui class (objectName of the top level widget in QtDesigner).
For example, if QtDesigner looks like that:
You'll get a class names Ui::CalculatorForm, whatever the .ui file name is.
Replace Ui::mydialog by Ui::Dialog (or whatever the class name is in your generated ui_mydialog.h file)
This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 8 years ago.
I am trying to make a class in Qt that will change things on my MainWindow.
This is my code so far:
MainWindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "message_function.h"
message_function MeFu;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
MeFu.Print(ui);
}
MainWindow::~MainWindow()
{
delete ui;
}
MainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
message_function.h (My Class header)
#ifndef MESSAGE_FUNCTION_H
#define MESSAGE_FUNCTION_H
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMainWindow>
namespace Ui { class MainWindow; }
class message_function
{
public:
void Print(Ui::MainWindow *ui);
};
#endif // MESSAGE_FUNCTION_H
message_function.cpp (My Class .cpp)
#include "message_function.h"
void message_function::Print(Ui::MainWindow *ui)
{
ui->label->setText("This is a test");
}
When i buld my project i get this error:
mainwindow.obj:-1: error: LNK2019: unresolved external symbol "public: void __thiscall message_function::Print(class Ui::MainWindow *)" (?Print#message_function##QAEXPAVMainWindow#Ui###Z) referenced in function "public: __thiscall MainWindow::MainWindow(class QWidget *)" (??0MainWindow##QAE#PAVQWidget###Z)
I don't know if i am doing this correctly or if i am doing something wrong.
Can somebody help me get this to work?
Sailordi, the error is in the way you're including the header files.
I did some modifications that should fix it. However I'd like to tell you that what you're doing is not a good programming practice.
MainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui { class MainWindow; }
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
MainWindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "message_function.h"
message_function MeFu;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
MeFu.Print(ui);
}
MainWindow::~MainWindow()
{
delete ui;
}
message_function.h
#ifndef MESSAGE_FUNCTION_H
#define MESSAGE_FUNCTION_H
#include "mainwindow.h"
class message_function
{
public:
void Print(Ui::MainWindow *ui);
};
#endif // MESSAGE_FUNCTION_H
message_function.cpp
#include "message_function.h"
#include "ui_mainwindow.h"
void message_function::Print(Ui::MainWindow* ui)
{
ui->label->setText("This is a test");
}
You shouldn't really be trying to access the MainWindow UI from another class, a better solution is to create a signal in your class that you want to make the change from and a slot in MainWindow that executes the change.
So I've been developing a small ACARS (plane tracking system) for the VATSIM network and apart of this entails using the FSUIPC library to interact with Flight Sim X. I have the program working beautifully in VS2010 but when I attempt to use any functions from FSUIPC I get the error:
"mainwindow.obj:-1: error: LNK2019: unresolved external symbol FSUIPC_Open referenced in **function "public: void __cdecl MainWindow::connectFSUIPC(void)" (?connectFSUIPC#MainWindow##QEAAXXZ)"**
From some research I've learnt that this is a library load error and for the life of me I can't figure out how to get it working. Below is the code for all files I am using.
AcarsTest.pro
#-------------------------------------------------
#
# Project created by QtCreator 2013-12-16T23:56:06
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = AcarsTest
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
INCLUDEPATH += "D:/Windows/QT/Tools/QtCreator/bin/AcarsTest/"
LIBS += -LD:/Windows/QT/Tools/QtCreator/bin/AcarsTest/ -lFSUIPC_User
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <Windows.h>
#include <FSUIPC_User.h>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
void connectFSUIPC();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
main.cpp
#include <Windows.h>
#include "mainwindow.h"
#include <QApplication>
#include "FSUIPC_User.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::connectFSUIPC()
{
DWORD dwResult;
if(FSUIPC_Open(SIM_ANY, &dwResult))
{
}
}
try change code like this
your code:
LIBS += -LD:/Windows/QT/Tools/QtCreator/bin/AcarsTest/ -lFSUIPC_User
replace by
win32:LIBS += "D:/Windows/QT/Tools/QtCreator/bin/AcarsTest/FSUIPC_User.lib"
This question should hopefully be easy to answer. I created a few buttons in my MainWindow using Qt Creator, and when I go to write the functions for the buttons, the compiler says they were not declared in this scope. What do I need to #include for these objects to be declared? The compiler error for the following would be 'baseDir' was not declared in this scope. baseDir is the objectName for a lineEdit in my window.
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "ui_mainwindow.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
public slots:
void getDir();
void createProj();
private slots:
void on_findDir_clicked();
void on_create_clicked();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "functions.h"
#include <QtGui/QApplication>
#include <QFileDialog>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_findDir_clicked()
{
QString path;
path = QFileDialog::getOpenFileName(
this,
"Choose a file to open",
QString::null,
QString::null );
baseDir->setText( path );
}
The items you define in your .ui file are not added directly to your main window class, they are added to its ui membre.
Try with:
ui->baseDir->setText( path );
Look at the ui_mainwindow.h file that is generated during the build if you're curious.