Hi, i'm new to Qt (started on: C- 5 years ago; C++ June last year; Qt - since this January) and i got some questions... i'm trying stuff for the past week but unsuccessful:
1. How can i make a struct visible in another class so i can create instances of it in diferent headers? (Like in mainwindow.h above) (This is a c++ question, i know..)
2. After i got it working, i saw in this forum that i got to add my struct to qt metatype so i can use signal/slot. I got to do it inside the header or the cpp file where i declared it?
I just made a clean project:
main.cpp (default code)
|-mainwindow.ui (just a start button on it)
|-mainwindow.h
|-mainwindow.cpp
|-myclass.h
|-myclass.cpp
mainwindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QString>
#include <QDebug>
#include "myclass.h"
namespace Ui {
class MainWindow;
}
struct myStruct{
int id;
float height;
};
{
Q_OBJECT
public:
explicit MainWindow
(QWidget *parent
= 0);
~MainWindow();
void startProgram();
signals:
void sendToClass(myStruct data[5]);
private slots:
void on_pushButton_clicked();
private:
void send();
Ui::MainWindow *ui;
myclass mcObj;
myStruct data[5];
};
#endif // MAINWINDOW_H
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QString>
#include <QDebug>
#include "myclass.h"
namespace Ui {
class MainWindow;
}
struct myStruct{
int id;
QString name;
float height;
};
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
void startProgram();
signals:
void sendToClass(myStruct data[5]);
private slots:
void on_pushButton_clicked();
private:
void send();
Ui::MainWindow *ui;
myclass mcObj;
myStruct data[5];
};
#endif // MAINWINDOW_H
To copy to clipboard, switch view to plain text mode
mainwindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::startProgram()
{
data[0].id = 1001;
data[0].name = "Mark";
data[0].height = 1.78f;
emit sendToClass(data);
}
void MainWindow::on_pushButton_clicked()
{
connect(this, &MainWindow::sendToClass, &mcObj, &myclass::receiveFromMain);
startProgram();
}
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::startProgram()
{
data[0].id = 1001;
data[0].name = "Mark";
data[0].height = 1.78f;
emit sendToClass(data);
}
void MainWindow::on_pushButton_clicked()
{
connect(this, &MainWindow::sendToClass, &mcObj, &myclass::receiveFromMain);
startProgram();
}
To copy to clipboard, switch view to plain text mode
myclass.h:
#ifndef MYCLASS_H
#define MYCLASS_H
#include <QObject>
{
Q_OBJECT
public:
explicit myclass
(QObject *parent
= 0);
void startStuff();
public slots:
void receiveFromMain();
};
#endif // MYCLASS_H
#ifndef MYCLASS_H
#define MYCLASS_H
#include <QObject>
class myclass : public QObject
{
Q_OBJECT
public:
explicit myclass(QObject *parent = 0);
void startStuff();
public slots:
void receiveFromMain();
};
#endif // MYCLASS_H
To copy to clipboard, switch view to plain text mode
myclass.cpp:
#include "myclass.h"
{
}
void myclass::startStuff()
{
}
void myclass::receiveFromMain()
{
//Want to receive the struct here!
}
#include "myclass.h"
myclass::myclass(QObject *parent) : QObject(parent)
{
}
void myclass::startStuff()
{
}
void myclass::receiveFromMain()
{
//Want to receive the struct here!
}
To copy to clipboard, switch view to plain text mode
I left it in blank because i just don't know how to make it!
Basically, in my real project i want to be able to receive position parameters from a socket class running with QtConcurrent, and pass it to several classes.. i could just pass: int id, QString Name, float x, float y, float z.... but it is much more clean with a struct or class!
that struct can be anywhere (or even transform it to a class, better for me..).. i just want it to be seen in almost every project files.. although i don't want to create global variables.. I just tried several ways and i can't do it.
Thank you in advance!
Bookmarks