Ui::HelloQtClass ui;
What is the meaning of this line in the following code of QT c++ application:
class HelloQt : public QMainWindow
{
Q_OBJECT
public:
HelloQt(QWidget *parent = 0);
~HelloQt();
private:
Ui::HelloQtClass ui;
qint32 port;
private slots:
void exit();
void OnClick();
};
#endif // HELLOQT_H
Ui::HelloQtClass ui; declares the member variable ui of type Ui::HelloQtClass. HelloQtClass is a class in the namespace Ui. This class will be defined in ui_helloqt.h, which is created only after you build your project. (To quickly access the file, select Ui::HelloQtClass and press F2, if you're using QtCreator.) This class is generated from helloqt.ui, which is generated from QtCreator's Designer, if you're using that.
The ui object declares a User Interface for this class, which, because it's derived from QMainWindow, is probably what defines what the application's main window looks like, assuming you're following general Qt standards.
Related
We are developing in C++ (and Qt) using Visual Studio 2015 and the Qt Designer for our UI (via Form / .ui files).
We now need to share some Common Data between our UI elements (e.g. most recently used paths etc.) and I would like to do this via dependency injection (i.e. providing the UI with a Reference to the common object during construction) instead of e.g. (ab)using the singleton pattern.
Has someone faced (and solved) similar problems, and is there a sensible way to do this?
Update (to elaborate):
For example I have a custom FooWidget which I wish to use in my FooDialog.ui form file.
// My custom Widget
class FooWidget : public QWidget {
Q_OBJECT
public:
FooWidget(QWidget* parent);
//...
}
// The FooDialog class (scaffolding created by Qt Designer)
class FooDialog : public QDialog {
Q_OBJECT
public:
FooDialog(QWidget* parent) : QDialog(parent), ui (new Ui::FooDialog()) {
ui->setupUp(this);
//...
}
private:
Ui::FooDialog* ui;
}
// The Ui::FooDialog class autogenerated(!) by Qt Designer
// I cannot (must not) change this code, as it will be regenerated every time
class Ui_FooDialog {
public:
FooWidget* widget;
void setupUi(QWidget *fooDialog) {
//...
widget = new FooWidget(fooDialog);
//...
}
}
namespace Ui { class ScannerStatus: public Ui_ScannerStatus {}; }
I would like to provide the FooWidget with a common data object (e.g. text size and colours shared across all my Ui classes), but I can't do so in the constructor (since the autogenerated Ui_FooDialog treats FooWidget as a generic QWidget, which only needs/takes a QWidget* parent in the constructor - I cannot provide a pointer or reference to my shared TextColourAndSize object.
I am aware I could create a second ui->widget->setupTextColourAndSize(...) step in FooDialog (after the initial ui->setupUi(this)) which provides that common data object, but having two init() type functions seems like a rather bad code smell (one is bad enough).
FooWidget needs two constructors, and a setter for the dependency:
explicit FooWidget(QObject *parent = nullptr) : QWidget(parent), … {
…
}
FooWidget(Dependency *dep, QObject *parent = nullptr) : FooWidget(parent) {
setDependency(dep);
}
void setDependency(Dependency *dep) {
…
}
Then you’d set the dependency after the widget is constructed:
FooDialog(Dependency *dep, …) … {
setupUi(this);
ui->fooWidget->setDependency(dep);
}
This could be automated: the parent widget can have a property that holds the pointer to the dependency, and the child widgets can find it automatically:
FooDialog(Dependency *, …) : … {
setProperty("dependency", dep);
setupUi(this);
}
FooWidget(QWidget *parent) : … {
auto *dep = parent() ? parent()->property("dependency").value<Dependency*>() : nullptr;
if (dep) setDependency(dep);
}
This will work with no extra effort if Dependency derives from QObject. Otherwise, you’ll need to have the following in a suitable header file:
class Dependency { … };
Q_DECLARE_METATYPE(Dependency*)
In all circumstances, you do need to promote the fooWidget object to FooWidget class within Qt Designer.
Ok from what I'm seeing you do not need "dependency injection". Question was incorrectly stated.
You can use this custom widget directly from Qt designer.
When you create your FooDialog place regular widget QWidget in place where you need to have a FooWidget.
Then "promote" regular this widget to FooWidget (possibly you have to add some simple information about that type) - (I did that long time ago and do not remember all details).
For detailed instruction just google: qt promote widget qt designer, you will find lots of examples how to do it.
These were good solutions, but talking about dependency injections, there is also an option to have some fun with C++. It's not a wise solution at all, of course I know it, but nevertheless...
foowidget.h
#ifndef FOOWIDGET_H
#define FOOWIDGET_H
#include <QWidget>
class Something
{
public:
QString getHello() const
{ return "Hello world!"; }
};
/***************************************************/
template<typename T>
class Injector
{
public:
QString getHello() const
{ return m_dataContainer.getHello(); }
private:
T m_dataContainer;
};
/***************************************************/
class FooWidget : public QWidget, public Injector<Something>
{
Q_OBJECT
public:
explicit FooWidget(QWidget* parent = nullptr);
protected:
virtual void mousePressEvent(QMouseEvent*) override;
};
#endif // FOOWIDGET_H
foowidget.cpp
#include "foowidget.h"
#include <QMessageBox>
FooWidget::FooWidget(QWidget *parent)
: QWidget(parent)
{ }
void FooWidget::mousePressEvent(QMouseEvent*)
{
QMessageBox::information(nullptr, "Test", getHello());
}
foodialog.h
#ifndef FOODIALOG_H
#define FOODIALOG_H
#include <QDialog>
class SomethingElse
{
public:
QString getHello() const
{ return "OMG! OMG"; }
};
#include "foowidget.h"
namespace Ui {
class FooDialog;
}
class FooDialog : public QDialog, public Injector<SomethingElse>
{
Q_OBJECT
public:
explicit FooDialog(QWidget *parent = nullptr);
~FooDialog();
protected:
void showEvent(QShowEvent *) override;
private:
QScopedPointer<Ui::FooDialog> ui;
};
#endif // FOODIALOG_H
foodialog.cpp
#include "foodialog.h"
#include "ui_foodialog.h"
#include <QMessageBox>
FooDialog::FooDialog(QWidget *parent)
: QDialog(parent)
, ui(new Ui::FooDialog)
{
ui->setupUi(this);
}
FooDialog::~FooDialog()
{ }
void FooDialog::showEvent(QShowEvent *)
{
QMessageBox::information(nullptr, "Test", getHello());
}
Multiple inheritance + deriving widgets from some small template proxy class works for both custom widgets and those, having UI forms. I've put a FooWidget on a FooDialog (via propagation mechanizm) in the sketch above and got two message boxes.
The idea itself can be implemented better, with smarter template usage, just tried to reduce sample code, anyway it's an unnessesary complication. But technically it works without any additional initializations =)
I have a problem regarding my QT project. I want to create a touchable Qlabel, so I created a placeholder in the gui designer and referenced it to my "watchlistlabel" which extends from QLabel. When I now start my app I get the error " no matching function for call to WatchListLabel::WatchListLabel(QGroupBox*&)'
name1 = new WatchListLabel(groupBox_3);
My labels are in a groupbox does this affect my code?
why it cant find a matching function?
^
#include<QLabel>
class WatchListLabel: public QLabel
{
Q_OBJECT
private:
void mousePressEvent(QMouseEvent * event) override;
public:
WatchListLabel();
};
watchlistlabel.cpp
#include "watchlistlabel.h"
void WatchListLabel::mousePressEvent(QMouseEvent *event)
{
}
WatchListLabel::WatchListLabel()
{
}
QTdesigner
You are missing a constructor which takes parent widget. General advice against this is to create new QObject subclasses using Qt Creator new class wizard, it will get everything right. But to solve the problem with this class, try replacing your default constructor with this constructor:
Declaration in .h:
explicit WatchListLabel(QWidget *parent = nullptr);
Definition in .cpp:
WatchListLabel::WatchListLabel(QWidget *parent)
: QLabel(parent)
//, any other member variable initializations
{
// any constructor code if needed
}
I just want to make a custom Dialog, so I want to make a class around the standard QDialog. The goal is to call the constructor which creates the Dialog, and the show() function should be called to make it shown. Next step would be to make a connect between my Widget (which calls the Dialog constructor) Pushbutton and the show() function.
My header looks like this:
#include <QDialog>
class Dialog_Setting : public QDialog
{
Q_OBJECT
public:
Dialog_Setting();
public slots:
void show(void);
private:
QDialog * dialog;
};
my .cpp:
#include "Dialog_Setting.h"
Dialog_Setting::Dialog_Setting()
{
dialog = new QDialog;
}
void Dialog_Setting::show()
{
dialog->show();
}
I have taken out my connect and get a new error.
What is wrong with my class?
undefined reference to `vtable for Dialog_Setting'
thanks for your help, I love StackOverflow
Make sure that show() is implemented as a slot so you can connect() stuff to it:
#include <QDialog>
class Dialog_Setting : public QDialog
{
Q_OBJECT
public:
Dialog_Setting();
public slots:
void show();
};
You also forgot to inherit from QObject or some other QObject-based class like QDialog and to declare the macro Q_OBJECT. All of these things are required to make your custom classes communicate with other classes through connect().
A have a class, inherited from QWidget and Ui_Form (automaticaly generated class, appears when you create a .ui in Qt). It looks like
class MyClass: public QWidget, public Ui_Form {}
Ui_Form has some members, which connected with relevant widgets in the .ui file (for example, QLineEdits, QButtons, etc).
class Ui_Form {
public:
QLineEdit *fileNameEdit;
void setupUi(QWidget *Form) {
fileNameEdit = new QLineEdit(layoutWidget);
fileNameEdit->setObjectName(QStringLiteral("fileNameEdit"));
}
}
As MyClass is inherited from Ui_Form, I can use this membes. But, when I try to do something, I have an exeption "Access violation reading location". For example:
fileNameEdit->setText("String");
Can somebody give an advice?
The way you are incorporating the Ui_Form part is not how Qt proposes it by default. If you look into this button example you can see how the ui part is incorporated diferently:
Header file
#ifndef BUTTON_H
#define BUTTON_H
#include <QWidget>
namespace Ui {
class Button;
}
class Button : public QWidget
{
Q_OBJECT
public:
explicit Button(int n, QWidget *parent = 0);
~Button();
private slots:
void removeRequested();
signals:
void remove(Button* button);
private:
Ui::Button *ui;
};
#endif // BUTTON_H
CPP code
#include "button.h"
#include "ui_button.h"
Button::Button(int n, QWidget *parent) :
QWidget(parent),
ui(new Ui::Button)
{
ui->setupUi(this);
ui->pushButton->setText("Remove button "+QString::number(n));
addAction(ui->actionRemove);
connect(ui->actionRemove,SIGNAL(triggered()),this,SLOT(removeRequested()));
connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(removeRequested()));
}
Button::~Button()
{
delete ui;
}
void Button::removeRequested()
{
emit remove(this);
}
The main difference is that I believe you are not calling Ui_From::setupUi function. It is clear to me that you do not need to follow the Qt suggested template (incorporating the ui as a class member rather than inheriting from it), however, it is much clearer from my point of view if you follow the Qt suggestions.
I have a class MyClass with:
- private:
pushButton *button;
void connectSignalAndSlot();
- private slot:
void buttonAction();
I want to connect these in MyClass using connectSignalAndSlot(), like so:
void MyClass::connectSignalAndSlot()
{
QObject::connect(button,SIGNAL(clicked()),this,SLOT(buttonAction()));
}
This gives me an error of
no matching function for call to 'QObject::connect(QPushButton*&, const char*, MyClass* const, const char*)';
If I inherit QObject with MyClass, the program compiles and starts, but then I get the following issues displayed in my Application Output pane:
QObject::connect: No such slot QObject::buttonAction() in ..\MyProject\myclass.cpp:48
Do I have to make the button and slot public and use them in the MainWindow class only? Is there no way to keep this at the MyClass level?
Thanks for your help!
You must have MyClass inherit from QObject AND add Q_OBJECT macro in your MyClass definition (header file) to have slots/signals work.
class MyClass : public QObject
{
Q_OBJECT
public:
....
};
Inheriting QObject is the right way, but your still missing Qt-Meta Object Code. Your header-file for your class should look like this:
#ifndef MYCLASS_H
#define MYCLASS_H
class MyClass : public QObject {
Q_OBJECT
// your methods, variables, slots and signals
}
#endif
Don't forget to create the moc file, the easiest way is to use qmake or the QtCreator IDE.