I am getting a mysterious error trying to subclass QTreeWidget. Below is code from the relevant files. In QtDesigner, I have promoted a QTreeWidget to a treeWidget, but I get the following error:
Error 1 error C2061: syntax error : identifier 'treeWidget' Visual Studio 2010\Projects\hw2\QTOpenGL\GeneratedFiles\ui_opengldemo.h 72 1 QTOpenGL
I have been told not to edit code in the ui_ files, so I'm assuming this problem can be solved without going in there. Any ideas? Does the code below give enough information to solve this issue? Thanks.
**treeWidget.h:**
#ifndef TREEWIDGET
#define TREEWIDGET
#include <QTreeWidget>
#include "gNode.h"
class treeWidget :
public QTreeWidget
{
Q_OBJECT
public:
treeWidget(QWidget*);
~treeWidget(void);
public slots:
void topLevelItem(gNode* node);
};
#endif
**treeWidget.cpp**
#include "treeWidget.h"
treeWidget::treeWidget(QWidget* parent) : QTreeWidget(parent)
{
}
treeWidget::~treeWidget(void)
{
}
void treeWidget::topLevelItem(gNode* node){
addTopLevelItem(node);
}
Figured it out. Changed name to myTreeWidget from treeWidget and all works fine.
Related
I am using Qt 5.7.1 (5.8) and when I try to compile my project, I get an error:
Error: C2664: "void Ui_OverviewHistogram::setupUi(QFrame *)" :
Converting from argument 1 of "OverviewHistogram *const " to "QFrame
*" not possible
(translated from german)
and I have no idea why... The strange thing is, that a lot of seamingly similar classes compile without any issue, just this one will not work. I tried to change the ui to a pointer, but it did not work.
The ui_overviewhistogram component of this class is a simple QFrame object designed completely in the Qt designer without any outside manual code 'hacking'.
I am relatively new to C++ and Qt and I can't figure out a solution (not even why this is a problem), can anyone help me with it?
My code:
overviewhistogram.h
#ifndef OVERVIEWHISTOGRAM_H
#define OVERVIEWHISTOGRAM_H
#include <QFrame>
#include <QColormap>
#include "AnalysisSession.h"
#include "ui_overviewhistogram.h"
namespace Ui {
class OverviewHistogram;
}
class OverviewHistogram : public QFrame
{
Q_OBJECT
public:
OverviewHistogram(Examination *examination, double max = 400, int w = 175, QWidget *parent = 0);
...
}
#endif // OVERVIEWHISTOGRAM_H
overviewhistogram.cpp
OverviewHistogram::OverviewHistogram(Examination *examination, double max, int w, QWidget *parent) :
QFrame(parent)
{
ui.setupUi(this);
...
}
I am having a hard time subclassing Widgets in the QDesigner. I am using QDesigner to create my UI, but using cmake to compile rather than .pro files. So I am basically using QT Creator for nothing other than generating ui files.
Now I want to subclass QLabel in order to override the mouse click event, so as far as I understand all I have to do is right click the QLabel and select "promote to". It then asks me what i want to promote to, so I say "clickable_qlabel.h". However, when I call "make", I get "ui_mainWindow.h:95:5: error: ‘Clickable_QLabel’ does not name a type". Unfortunately I have no idea where I need to put clickable_qlabel.h, or whether it already exists and I just need to fill it with my code.
Any help would be greatly appreciated!
Many thanks.
[UPDATE]
OK, so now I have created the following class:
QLabelClickable.h
#ifndef _QLABELCLICKABLE_H_
#define _QLABELCLICKABLE_H_
#include <QLabel>
#include <QMouseEvent>
class QLabelClickable : public QLabel
{
Q_OBJECT
public:
explicit QLabelClickable( const QString& text="", QWidget* parent=0 );
~QLabelClickable();
signals:
void clicked(int, int);
protected:
void mousePressEvent(QMouseEvent* event);
};
#endif
QLabelClickable.cpp
#include "QLabelClickable.h"
QLabelClickable::QLabelClickable(const QString& text, QWidget* parent)
: QLabel(parent)
{
setText(text);
}
QLabelClickable::~QLabelClickable()
{
}
void QLabelClickable::mousePressEvent(QMouseEvent* event)
{
emit clicked(event->x(),event->y());
}
So this code compiles beautifully. So now I am in QtDesigner and I create a QLabel, called label4, and I right click and select "Promote to". Then under "Promoted class name:" I type "QLabelClickable" and under "Header file:" I type "QLabelClickable.h". Then I click "Promote". Wonderful. But I am still getting the error:
Vigil/build/ui_mainWindow.h:328:42: error: no matching function for call to ‘QLabelClickable::QLabelClickable(QWidget*&)’ label_4 = new QLabelClickable(tab);
So clearly QtDesigner needs to be instructed (somehow) where my implementation of QLabelClickable is. Quite frustrating.
Eh. My error, I should have read the error message properly. I hadn't included a constructor for the passing of only a QWidget. Adding
QLabelClickable::QLabelClickable( QWidget* parent ) : QLabel(parent) {
}
to my CPP has solved the problem! Hooray.
First of all, i've seen some solutions, but i didn't understand them. I am a total Newbie in QT and even Google didn't helped me. English is not my first language
This is the error message after debugging in QT Creator 5.6
C2143: syntax error: missing ';' before '*'
C4430: missing type specifier - int assumed. Note: C++ does not
support default-int
C2238: unexpected token(s) preceding ';'
here is the file
secdialog.h
#ifndef SECDIALOG_H
#define SECDIALOG_H
#include <QDialog>
#include "tridialog.h"
#include "quadialog.h"
namespace Ui {
class SecDialog;
}
class SecDialog : public QDialog
{
Q_OBJECT
public:
explicit SecDialog(QWidget *parent = 0);
~SecDialog();
private slots:
void on_pushButton_5_clicked();
void on_status_clicked();
void on_pushButton_2_clicked();
void on_zuteilung_clicked();
private:
Ui::SecDialog *ui;
TriDialog *triDialog; //this is the line with the problem
QuaDialog *quaDialog; // funny thing, he didn't complain about this line
};
#endif // SECDIALOG_H
Sorry if the problem was already here in the questions, but i didn't understand the other solutions.
I'm trying to make a little program just for show, it only needs to show some forms and thats it. There are some buttons and every button opens another form and hides the one before.
My problem is, i always have my problems with programming and languages, often i don't know enough to understand a solution, i'm more a "someone shows me how and then i use it and understand it" - i learn through "copy paste"
Could anyone tell me what i did wrong?
If you need more information about the project, just ask.
Thank you in advance.
Here the the requested tridialog.h
#ifndef TRIDIALOG_H
#define TRIDIALOG_H
#include <QDialog>
#include "secdialog.h"
namespace Ui {
class TriDialog;
}
class TriDialog : public QDialog
{
Q_OBJECT
public:
explicit TriDialog(QWidget *parent = 0);
~TriDialog();
private slots:
void on_pushButton_5_clicked();
private:
Ui::TriDialog *ui;
TriDialog *triDialog;
};
#endif // TRIDIALOG_H
Here is the whole project for qt kraftwerk.zip
Your problem is in tridialog.h:
#include "secdialog.h"
When you include secdialog.h, you bring in your SecDialog class declaration, which has a TriDialog pointer (on the problematic line) before the TriDialog class is declared!
Since you don't actually need anything from secdialog.h in tridialog.h, just remove
#include "secdialog.h"
and it should work.
It looks like you have a scoping error, unless I'm misunderstanding your intent. This code:
namespace Ui {
class TriDialog;
}
class TriDialog : public QDialog
{};
declares a class named TriDialog in the namespace Ui and then declares a class named TriDialog outside the namespace Ui. You now have two classes, Ui::TriDialog and TriDialog. You need to put the whole definition inside the namespace scope.
Edit:
Looks like skearney found the error you were asking about, but this is still an issue.
This is about a Qt 5.3.2 project buildt using CMake.
I have designed a QMainWindow using the Qt Designer, leading
to main.ui.
CMakeLists.txt (the almost complete thing may be
found here where I already posted it for a different question:
Linking and UIC order in a CMake Qt project )
already takes care of calling UIC so I have my hands on ui_main.h.
ui_main.h offers the class Ui::MainWindow with the plain form information
where all the buttons and stuff should be and the method *void setupUi(QMainWindow MainWindow).
Now my workflow (is it even a feasible one?) goes like this:
I build a totally new header file Form_main.h:
// Form_main.h
[..]
class Form_main : public MainWindow, public QMainWindow
{
Q_OBJECT
privat slots:
void on_some_event();
[..]
};
The class uses said auto-generated MainWindow::setupUi(this) to 'get in shape' and QMainWindow to be, well, a QMainWindow with all that stands for.
But now I am in a dilemma: Either I remove the Q_OBJECT macro call leading to connect(..) no longer recognizing that Form_main has signal slots, or
I keep the Q_OBJECT leading to the infamous
undefined reference to `vtable for display::Form_main'
error while linking the project.
Now, there have been, in fact, people with similar issues.
Naming some links:
http://michael-stengel.com/blog/?p=103
Qt Linker Error: "undefined reference to vtable"
Undefined reference to vtable... Q_OBJECT macro
Qt vtable error
A hint I got from the last one: "MOC must generate code for ui_main.h and the generated code must be compiled and linked."
In any case, these answers all seem to boil down to 'running qmake again'. Well, I use CMake all the way wanting my project to configure and compile after exactly
cmake .
make
What I did try was deleting everything in and below the build directory
(including every auto-generated file) and then running cmake . && make;.
Sadly that did not help. I am afraid this is my second noob question today... would you bear with me once more?
=== AFTER TRYING GREENWAYS ANSWER I PROVIDE MORE DETAILS. ===
Here is the autogenerated ui_main.h
/********************************************************************************
** Form generated from reading UI file 'main.ui'
**
** Created by: Qt User Interface Compiler version 5.3.2
**
** WARNING! All changes made in this file will be lost when recompiling UI file!
********************************************************************************/
#ifndef UI_MAIN_H
#define UI_MAIN_H
#include <QtCore/QVariant>
#include <QtWidgets/QAction>
[.. more Widget Includes ..]
QT_BEGIN_NAMESPACE
class Ui_MainWindow
{
public:
QAction *action_exit;
[.. more sub widgets like that .. ]
void setupUi(QMainWindow *MainWindow)
{
[ .. Setting up the form. Harmless code. .. ]
} // setupUi
void retranslateUi(QMainWindow *MainWindow)
{
[ .. completely harmless .. ]
} // retranslateUi
};
namespace Ui {
class MainWindow: public Ui_MainWindow {};
} // namespace Ui
QT_END_NAMESPACE
#endif // UI_MAIN_H
Reading all this and incorporating your post right now I am at
// form_main.h
#ifndef MHK_FORM_MAIN_H
#define MHK_FORM_MAIN_H
#include <QMainWindow>
#include "ui_main.h"
[..]
namespace Ui { class MainWindow; }
namespace display
{
class Form_main : public QMainWindow
{
Q_OBJECT
private:
ostream* stdout;
ostream* stderr;
Ui::MainWindow* uiMainWindow;
/** Called by the constructor. Sets up event connections and other
* preliminary stuff the qt Designer is overtasked with. */
void setup_form();
[..]
public:
explicit Form_main(QWidget* parent = 0);
~Form_main();
private slots:
void exit_program();
};
}
#endif
And my cpp
// form_main.cpp
#include "ui_main.h"
#include "form_main.h"
[..]
using namespace Ui;
namespace display
{
void Form_main::setup_form()
{
QObject::connect(uiMainWindow->action_exit, SIGNAL(triggered()), this, SLOT(exit_program()));
[..]
}
Form_main::Form_main(QWidget* parent) : QMainWindow(parent)
{
uiMainWindow = new Ui::MainWindow();
uiMainWindow->setupUi(this);
[..]
#if defined(Q_OS_SYMBIAN)
this->showMaximized();
#else
this->show();
#endif
}
Form_main::~Form_main()
{
delete uiMainWindow;
}
[..]
Form_main::exit_program()
{
this->close();
(*stdout) << "Thanks for playing " << getProgramName() << endl;
}
}
Ok. I see (partly) the problem. Just create a widget class like this:
.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
.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;
}
This is how the QtCreator creates ui-Widgets. "ui_MainWindow.h" is your generated .h file.
Thanks for all your help! However, the problem was in CMakeLists.txt after all. The comment of Chris Morlier on Undefined reference to vtable pointed me to the solution.
The pertinent passage goes:
For Qt users: you can get this same error if you forget to moc a header
I simply had to add the header form_main.h into this line:
QT5_WRAP_CPP(qt_H_MOC ${qt_H} "${DIR_SRC}/include/form_main.h")
I try to include header window into his child window. Let see a code:
//header of main window
#include "second_window.h" // include child window
class mainclass : public QMainWindow
{
Q_OBJECT
public:
mainclass(QWidget *parent = 0);
~mainclass();
second_window *h_secondwindow = nullptr;
private:
Ui::mainclass ui;
};
And Second
//header second_window.h
#include "mainwindow.h" // only this from mainwindow class
class second_window : public QWidget
{
Q_OBJECT
public:
third_window * h_third_window = nullptr;
second_window(QWidget *parent = 0);
~second_window();
private:
void reDrawTable();
Ui::second_window ui;
};
#endif // second_window_H
And when in "second_window.h" i try to include "header of main window" (lets call it mainwindow.h )
I got error by this line (in main window) :
second_window *h_secondwindow = nullptr;
Errors like:
//qdatetime.h ( i dont edit this file ;0 )
error C2059: syntax error : '::'
static inline qint64 nullJd() { return std::numeric_limits<qint64>::min(); }
//mainwindow.h
error C2143: syntax error : missing ';' before '*'
second_window *h_secondwindow = nullptr;
I need this variable bcoz i have to operate in main window method on this child window.
Any idea?
Try, cross checking your "second_window.h" to see if there any code errors, it might be there are some few errors. Such errors mainly happen with pointers and references, so an error might be possible!
You shouldn't include the header files inside each other. You can use a forward declaration of mainwindow class in second_window.h :
//header second_window.h
class mainwindow;
Forward declarations mean that you can only use that class as a pointer in the header file and not a class instance. So using this way you can now have a pointer to your mainwindow in second_window.h.
But it is recommended to check if you can change your design and eliminate this circular dependency.
I think your code is dark means.
Why does second_window include main_window?
You should init in main window.h:
second_window* _secondWindowUi;
in .cpp file :
_secondWindowUI = new second_window();
It is easy!