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().
Related
I have a class ("controller" for example)
and in this class, I have created many objects of different other classes
with different parents.
How to send signal between that classes and "controller" to call a function in "controller" class?
#include "controller.h"
Controller::Controller(QObject *parent) : QObject (parent){
connect(sender(), SIGNAL(recivedCall(QString)), this, SLOT(alert(QString)));
}
void Controller::onCall(QJsonObject callinfo){
nodes[callinfo["username"].toString()]= new PanelManager();
nodes[callinfo["username"].toString()]->handleCallStateChanged(callinfo);
}
void Controller::alert(QString callinfo){
qDebug()<<callinfo;
}
For example, how to send signal from "recivedCall" in each "PanelManager" object to call "alert" function in "controller" class ?
The object which creates your two components has to set the connections between your signal and your slot. But, you shouldn't expose inner components (i.e. create getter to return a pointer on a attribute).
A way to tackle the last problem with Qt is to create a signal in your parent and let it broadcast the calls.
For example, if I need to connect a QCheckBox to a QLineEdit in two different widgets:
class Parent1: public QWidget
{
Q_OBJECT
public:
Parent1(): QWidget(), myCheckBox(new QCheckBox("Edit", this))
{
connect(myCheckBox, &QCheckBox::clicked, this, &Parent1::editForbidden);
}
private:
QCheckBox* myCheckBox;
signals:
void editForbidden(bool);
};
class Parent2: public QWidget
{
Q_OBJECT
public:
Parent2(): QWidget(), myLineEdit(new QLineEdit("Edit", this))
{
connect(this, &Parent2::forbidEdit, myLineEdit, &QLineEdit::setReadOnly);
}
private:
QLineEdit* myLineEdit;
signals:
void forbidEdit(bool);
};
// In the parent of Parent1 and Parent2 (or in the main if there is no parent)
QObject::connect(p1, &Parent1::editForbidden, p2, &Parent2::forbidEdit);
In this example, when I click on the checkbox in parent1, the lineEdit in parent2 is disabled. But, Parent1 doesn't know anything about Parent2.
I've created base class - MainWindow. In this class I create object of another class "SecondWindow". By using object of SecondWindow I want to use function() method that is defined in base class. How can I do this?
I've tried to transfer my base class as a parameter to SecondWindow object but that wasn't working (probably becouse in MainWindow I have included "secondWindow.h" and in SecondWindow I tried to include "mainwindow.h" header files). Is there way to simply use functions/variables of base class?
mainwindow.h
#include<QMainWindow>
#include "secondwindow.h"
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow();
SecondWindow *secondWindow;
void function();
}
mainwindow.cpp
#include "mainwindow.h"
MainWindow::MainWindow()
{
secondWindow = new secondWindow();
function();
}
void MainWindow::function()
{
qDebug()<<"yes";
}
secondwindow.h
class SecondWindow : public QMainWindow
{
Q_OBJECT
public:
SecondWindow();
}
secondwindow.cpp
#include "secondwindow.h"
SecondWindow::SecondWindow()
{
//here I want to use function();
}
You would want to inherit your MainWindow class, which would then inherit QMainWindow()
So in secondwindow.h you would change
class SecondWindow : public QMainWindow
{
Q_OBJECT
public:
SecondWindow();
};
to
#include "mainwindow.h"
class SecondWindow : public MainWindow
{
Q_OBJECT
public:
SecondWindow();
};
And you will then be able to use function() in secondwindow.cpp
Also, you have another issue at hand and it's that your mainwindow uses a pointer to second window, but you will then have a circular dependence issue. (You need SecondWindow in order to compile MainWindow, however, you want SecondWindow to be able to use function()).
To resolve this, you need to forward-declare SecondWindow in your MainWindow class.
In mainwindow.h remove #include "secondwindow.h" and replace it with class SecondWindow;
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'm trying to connect a signal and a slot. I had it working, but I accidentally deleted a .h file. Now I tried to rewrite it, and everything's gone to hell. I've got:
#ifndef GAMEMANAGER_H
#define GAMEMANAGER_H
#include "gamepersistence.h"
class GameManager
{
Q_OBJECT
public:
GameManager();
~GameManager();
GamePersistence* _gamePersistece;
// other stuff
signals:
void refreshPlease();
void gameOverSignal();
};
#endif // GAMEMANAGER_H
And then I'm trying to connect it in another class:
GameWindow::GameWindow(QWidget *parent)
: QWidget(parent)
{
setFixedSize(900,200);
setWindowTitle(trUtf8("Amőba"));
//this->setStyleSheet("background-color: white;");
_gameManager = new GameManager();
// _gameManager->setFocusPolicy(Qt::StrongFocus);
connect(_gameManager, SIGNAL(gameOverSignal()), this, SLOT(gameOver()));
connect(_gameManager, SIGNAL(refreshPlease()), this, SLOT(refreshTable()));
//other stuff
}
This is in a class called GameWindow. Now I'm getting errors for the two connect lines:
error: no matching function for call to 'GameWindow::connect(GameManager*&, const char*, GameWindow* const, const char*)'
connect(_gameManager, SIGNAL(gameOverSignal()), this, SLOT(gameOver()));
What did I mess up in the header? I think I've rewritten it as it was...
Figured it out, I have to use the : public QObject base class.
in gamemanager.h add the public inheritance from QObject for signal and slot can be called.
class GameManager : public QObject{ //your class definition };
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.