use a signal between 2 classes Qt - c++

I have a MainWindow class which contain a QComboBox and a widget which is from another class. This second class contain a QCheckBox and a QComboBox. I want to use a signal to change the checkState of my QCheckBox and the string displayed in my QComboBox from my widget class when the string displayed in my QComboBox from my MainWindow has changed.
But I don't really understand which form my signal must have and how I can use it in my widget class.
MainWindow.h :
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QWidget>
#include <QComboBox>
#include "devices_left_widget.h"
#define STRING_DEVICE1 "DEVICE1"
#define STRING_DEFAULT ""
class MainWindow : public QMainWindow {
Q_OBJECT
public:
explicit MainWindow(QWidget* parent = nullptr);
signals:
public slots:
void carte_OK();
protected:
QComboBox* carte_type_combo_box;
devices_left_widget* left_widget;
};
#endif // MAINWINDOW_H
device_left_widget.h :
#ifndef DEVICE_LEFT_WIDGET_H
#define DEVICE_LEFT_WIDGET_H
#include <QWidget>
#include <QCheckBox>
#include <QComboBox>
#define STRING_DEVICE1 "DEVICE1"
#define STRING_DEFAULT ""
class device_left_widget : public QWidget {
Q_OBJECT
public:
explicit device_left_widget(QWidget* parent = nullptr);
signals:
public slots:
protected:
QGridLayout* main_grid_layout;
QCheckBox* device_checkbox;
QComboBox* device_type_combo_box;
};
#endif // DEVICES_LEFT_WIDGET_H

Let's call your widget's name container. We want to connect QComboBox's currentTextChanged(const QString &text) signal to the widget, so we create a slot that corresponds to the signal, let it be chosenTextChanged(const QString& text). We connect them inside MainWindow constructor:
connect(ui->comboBox, SIGNAL(currentTextChanged(const QString &)),
ui->container, SLOT(chosenTextChanged(const QString &)));
And inside your container class, define the slot as public:
public slots:
void chosenTextChanged(const QString &text) {
//change your QCheckBox's state and
//change your QComboBox's text
}

Related

QT5 mainwindow slots not being discovered

As per a tutorial I'm following, I'm placing functions that I would like to call in my widget under public: Q_SLOTS in mainwindow.h. When I open mainwindow.ui in design mode, then click edit->edit signals/slots, and left click on a button and drag to the window,I don't see an option to associate the signal I created with one of the functions I specified myself.
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
public Q_SLOTS:
void SendBtnClicked();
void TypingChanged();
void LoginClicked();
void OnMessageReturn();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
If you want to connect the signal to your slot from there. you should add your slot to that slot list.
For Example, I write this slot in mainwindow.h
private slots:
void myExampleSlot();
that print something.
void MainWindow::myExampleSlot()
{
qDebug() << "I am here";
}
press Edit and then add your slot function :
then find this in the slots list and choose it.
out put :

How to add clicked signal and slot to custom button in qt

I have a Box class that inherits from QPushButton. I want to have a onClick event on the button by using connect (SIGNAL and SLOT) and call a custom function onClick() declared in box.h
box.h
#ifndef BOX_H
#define BOX_H
#include <QPushButton>
class Box : public QPushButton {
public:
Box(const QString& text, QWidget* parent = nullptr);
void onClick();
};
#endif // BOX_H
//box.cpp
#include "box.h"
Box::Box(const QString& text, QWidget* parent)
: QPushButton(text, parent)
{
connect(this, SIGNAL(clicked()), SLOT(this->onClick()));
}
void Box::onClick()
{
this->setText("Something");
}
your box needs the label for defining slots
class Box : public QPushButton
{
Q_OBJECT
public:
Box(const QString& text, QWidget* parent = nullptr);
//may be public or private
public slots:
void onClick();
};

How to display the input of QLineEdit in a dialog on the label of mainwindow?

I'm new to QtCreator and created a button on the mainwindow that opens a dialog. In the dialog i added QLineEdit and a button. When i click on this button, i want the input text to be displayed on a Qlabel in the mainwindow.
I found another question which is basicly the same, but for me it has to work the other way around.
So In the header of MainWindow.h i have added to public:
void setLabelText(QString str);
I created the getter/setter function and added it to MainWindow.cpp:
void MainWindow::setLabelText(QString str)
{
ui->label->setText(str);//it is label dialog
}
And in the windowdialog.cpp i have added under button click() slot:
void WindowDialog::on_pushButton_clicked()
{
QString str = ui->lineEdit->text();
MainWindow MainWindow;
MainWindow.setLabelText(str);
}
It compiles without errors. Yet when i click on the button in the dialog, it won't print the results in the mainwindow... I have been trying to find another examples about how to do this, but couldn't really find one yet. So what am i missing? Or how do i make this work?
You can use signals and slots for this. https://doc.qt.io/qt-5/signalsandslots.html
you would need to connect the signal to a slot by using this command
connect(dlg,&DialogWindow::transmit,this,&MainWindow::update);
anytime you want to call the update function you would just need to emit the signal.
emit transmit(ui->lineEdit->text());
I created a dialog window class in this example.
Dialog.h
#ifndef DIALOGWINDOW_H
#define DIALOGWINDOW_H
#include <QDialog>
QT_BEGIN_NAMESPACE
namespace Ui { class Dialog; }
QT_END_NAMESPACE
class DialogWindow : public QDialog
{
Q_OBJECT
public:
DialogWindow(QWidget *parent = nullptr);
~DialogWindow();
signals:
void transmit(QString txt);
private slots:
void on_buttonBox_accepted();
private:
Ui::Dialog *ui;
};
#endif // DIALOGWINDOW_H
Dialog.cpp
#include "dialog.h"
#include "ui_dialog.h"
DialogWindow::DialogWindow(QWidget *parent)
: QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
}
DialogWindow::~DialogWindow()
{
}
void DialogWindow::on_buttonBox_accepted()
{
emit transmit(ui->lineEdit->text());
}
MainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "dialog.h"
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void on_pushButton_clicked();
void update(QString txt);
private:
Ui::MainWindow *ui;
DialogWindow *dlg;
};
#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);
dlg = new DialogWindow(NULL);
connect(dlg,&DialogWindow::transmit,this,&MainWindow::update);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
dlg->show();
}
void MainWindow::update(QString txt)
{
ui->label->setText(txt);
}
In this line MainWindow MainWindow;, you create a new MainWindow, but you want to set the text on your existing MainWindow.
Instead you might want to pass a reference or pointer to the MainWindow to the dialog, once you create it.
If you pass your MainWindow as the parent of the QDialog, you could do it this way:
void WindowDialog::on_pushButton_clicked()
{
QString str = ui->lineEdit->text();
static_cast<MainWindow*>(parent())->setLabelText(str);
}
A safer version using dynamic_cast:
void WindowDialog::on_pushButton_clicked()
{
QString str = ui->lineEdit->text();
MainWindow* window = dynamic_cast<MainWindow*>(parent());
if (!window) {
throw std::runtime_error{"Parent was not a MainWindow"};
}
window->setLabelText(str);
}

Qt connection QTableWidget and QGLWidget

I am new in Qt and i stacked in a task. I created a QGLWidget and i try to connect it with a QTablewidget. I want to take a variable from QTableWidget which i want to use in order to plot in the QGLWidget. The problem is that there are two classes, one for QGLWidget and one for ui (QDialog where QTableWidget is included) and i don't know how to take input from QTableWidget. Can i use signal and slot or i could have access in ui from QGLWidget and how can i do it? I would appreciate any thoughts.
You can do this without signal and slot. Use setter, you can set different types of variables and use it inside GLWidget:
#ifndef GLWIDGET_H
#define GLWIDGET_H
#include <QGLWidget>
#include <QDebug>
class GLWidget : public QGLWidget
{
Q_OBJECT
public:
explicit GLWidget(QWidget *parent = 0);
void setValue(int i);
signals:
public slots:
private:
int member;
};
#endif // GLWIDGET_H
Cpp:
#include "glwidget.h"
GLWidget::GLWidget(QWidget *parent) :
QGLWidget
(parent)
{
}
void GLWidget::setValue(int i)
{
member = i;
qDebug() << i;
}
Usage:
void MainWindow::on_tableWidget_clicked(const QModelIndex &index)
{
GLWidget *wgt = new GLWidget;
wgt->setValue(index.data().toInt());
wgt->show();
}

Transmitting data between classes

My code is set up in this way: the main window with a QTableWidget, and a control panel created with a different class from the MainWindow. In the control panel control there is a QListWidget where I want to load the titles of the header from the table, but being QTableWidget private, how can I pass data between the two classes?
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include "QControlPanel.h"
#include <QMainWindow>
#include <QWidget>
#include <QtGui>
#include <QTableWidget>
namespace Ui {
class MainWindow;
class GenerateXML;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
public slots:
private:
Ui::MainWindow *ui;
QTableWidget* m_pTableWidget;
QControlPanel* preferences;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
this->setWindowIcon(QIcon("DrawItem.ico"));
this->setWindowTitle("DrawItem");
resize(890, 475);
m_pTableWidget = new QTableWidget(this);
m_TableHeader<<"A"<<"B"<<"C"<<"D"<<"E";
m_pTableWidget->setHorizontalHeaderLabels(m_TableHeader);
m_pTableWidget->resizeColumnsToContents();
m_pTableWidget->verticalHeader()->setVisible(false);
m_pTableWidget->move(30, 75);
m_pTableWidget->resize(410, 151);
preferences = new QControlPanel(this);
}
QControlPanel.h
#include <QDialog>
#include <QTableWidget>
#ifndef QCONTROLPANEL_H
#define QCONTROLPANEL_H
class QControlPanel : public QDialog
{
Q_OBJECT
public:
explicit QControlPanel(QWidget *parent = 0);
~QControlPanel();
public slots:
void customHeader(QTableWidget *table);
private:
QListWidget *headerlist;
private slots:
};
#endif
QControlPanel.cpp
#include "QControlPanel.h"
QControlPanel::QControlPanel(QWidget *parent) :
QDialog(parent)
{
headerlist = new QListWidget(inputHeader);
headerlist->setGeometry(140, 15, 140, 130);
}
You can have a signal in the QControlPanel class which retrieves the headers. So QControlPanel can be like :
class QControlPanel : public QDialog
{
Q_OBJECT
public:
explicit QControlPanel(QWidget *parent = 0);
~QControlPanel();
signals:
QStringList getHeaders();
private:
QListWidget *headerlist;
};
And connect the getHeaders() signal to a slot of the class which contains the list of columns to retrieve them. This should be done in the constructor of the MainWindow :
connect(preferences,SIGNAL(getHeaders()),this,SLOT(getTableHeader()));
getTableHeader() is a slot in MainWindow which return the header columns :
QStringList getTableHeader()
{
return m_TableHeader;
}
Now you can access the headers in QControlPanel :
QControlPanel::QControlPanel(QWidget *parent) :
QDialog(parent)
{
headerlist = new QListWidget();
headerlist->insertItems(0, getHeaders());
}