'use a signal between 2 classes Qt
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
Solution 1:[1]
This probably is not a problem. If a property isn't part of the type, then Typescript won't let you access that property. In this case, if you try to access age you will get a type error.
let personFromServer = {
name: "myName",
surname: "mySurname",
age: 16
}
let p: Person = personFromServer;
p.age // Property 'age' does not exist on type 'Person'.(2339)
It's important to remember that Typescript is a compile time tool to allow the developer to write more correct code more quickly. Typescript is not a way to enforce types of objects at runtime.
When compiled, all your above code turns into:
let personFromServer = {
name: "myName",
surname: "mySurname",
age: 16
}
let p = personFromServer;
console.log("I wish p to have only name and surname", p)
So there is no hint left in the executable code that could tell you what should be included and should not.
This might matter if there are security implications. For example, a user may have a hashed password that user is retrieved from the database that you don't want your server to return on its public API. Then you have to do this manually at runtime.
Perhaps something like:
import _ from 'lodash' // use lodash's omit() for simplicity
interface Person {
name: string
surname: string
}
interface PersonWithAge extends Person {
age: number
}
let personFromServer: PersonWithAge = {
name: "myName",
surname: "mySurname",
age: 16
}
let p: Person = _.omit(personFromServer, 'age')
console.log(p) // only name and surname
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Alex Wayne |
