So I have a simple class with one variable called param1, constructor, and two functions to set/get that parameter:
So here are the sources of ctester.h and ctester.cpp
#ifndef CTESTER_H
#define CTESTER_H
class CTester {
public:
CTester(); // constructor
void SetParam1(int value);
int GetParam1(void);
private:
int param1;
};
#endif // CTESTER_H
#ifndef CTESTER_H
#define CTESTER_H
class CTester {
public:
CTester(); // constructor
void SetParam1(int value);
int GetParam1(void);
private:
int param1;
};
#endif // CTESTER_H
To copy to clipboard, switch view to plain text mode
CTester::CTester(void)
{
// initializing all the parameters
param1 = 0;
}
void CTester::SetParam1(int value)
{
param1 = value;
}
int CTester::GetParam1(void)
{
return param1;
}
CTester::CTester(void)
{
// initializing all the parameters
param1 = 0;
}
void CTester::SetParam1(int value)
{
param1 = value;
}
int CTester::GetParam1(void)
{
return param1;
}
To copy to clipboard, switch view to plain text mode
My main.cpp and mainwindow.cpp files are rather simple, since I just started a QMainWindow application in QtCreator:
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
MainWindow w;
w.show();
return a.exec();
}
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
To copy to clipboard, switch view to plain text mode
What I am not quite sure is, what is the most proper way of getting/setting that param1 variable in my class through that MainWindow form?
The search on QtCentre gives tons of similar threads BUT about sharing data between dialogs or dialogs and main window, but I did not see any description in docs or the forum threads about reading/writing to own class.
In other words, I have my Qt GUI interface, but what I want to achieve now is create now personal c++ class which is "tied" to that GUI and updated by it. I do have two buttons on the MainWindow form, Set and get, and the editbox for setting the value and label for showing the value of param1 when get button is clicked.
Keep in mind that I want to keep ctester.h and ctester.cpp as c++ source files only, i.e. no Qt related macros/keywords there.
Now, does it mean that I need another "intermediate" wrapper class for this mechanism to work? Or there is a better, more elegant approach to achieve what I just described?
Bookmarks