Hi there! This is my first post to qt central. I'm stuck with my trials to save the contents of a Dialog in the calling MainWindow (MainWindow and Dialog created in QTcreator). The scenario: I have a MainWindow with a line edit, an integer spinbox and two buttons (one calls up the Dialog, the other one should call a public function that reads some private variables of class MainWindow and puts them into the line edit and the integer spinbox) and a Dialog with the same input Gadgets.
Plan was to write some public methods in class MainWindow that should have been used by class Dialog in order to store the values of the Dialog's input wigets in class MainWindow's private vars myTextString and myIntegerZahl.
Showing up the Dialog works, unless I call void setMyTextString(QString textstring); - in that case the program refuses to compile, throwing an error: "cannot call member function 'void MainWindow::setMyTextString(QString)' without object".
I did some reading on the Qt examples, but failed to find one that shows what I'm aiming at. Could somebody please show me the correct way of transfering the contents of my Dialog? Here comes the code:
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "dialog.h"
namespace Ui {
class MainWindow;
}
{
Q_OBJECT
public:
explicit MainWindow
(QWidget *parent
= 0);
~MainWindow();
void getMyTextString();
void setMyTextString
(QString textstring
);
void getMyIntegerZahl();
void setMyIntegerZahl();
private slots:
void on_btnDialog_clicked();
void on_btnUebertrag_clicked();
private:
Ui::MainWindow *ui;
Dialog *myDialog;
int myIntegerZahl;
};
#endif // MAINWINDOW_H
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "dialog.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
void getMyTextString();
void setMyTextString(QString textstring);
void getMyIntegerZahl();
void setMyIntegerZahl();
private slots:
void on_btnDialog_clicked();
void on_btnUebertrag_clicked();
private:
Ui::MainWindow *ui;
Dialog *myDialog;
QString myTextString;
int myIntegerZahl;
};
#endif // MAINWINDOW_H
To copy to clipboard, switch view to plain text mode
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
//#include "dialog.h"
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_btnDialog_clicked()
{
myDialog = new Dialog;
myDialog->exec();
ui->le_myTextString->setEnabled(true);
}
void MainWindow::on_btnUebertrag_clicked()
{
}
// Austausch-Methoden
void MainWindow
::setMyTextString(QString textstring
) {
myTextString = textstring;
}
#include "mainwindow.h"
#include "ui_mainwindow.h"
//#include "dialog.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_btnDialog_clicked()
{
myDialog = new Dialog;
myDialog->exec();
ui->le_myTextString->setEnabled(true);
}
void MainWindow::on_btnUebertrag_clicked()
{
}
// Austausch-Methoden
void MainWindow::setMyTextString(QString textstring)
{
myTextString = textstring;
}
To copy to clipboard, switch view to plain text mode
dialog.h
#ifndef DIALOG_H
#define DIALOG_H
#include "ui_dialog.h"
//#include <QDialog>
namespace Ui {
class Dialog;
}
class Dialog
: public QDialog,
public Ui
::Dialog{
Q_OBJECT
public:
Dialog();
private slots:
void on_btnSend_clicked();
private:
Ui::Dialog *ui;
};
#endif // DIALOG_H
#ifndef DIALOG_H
#define DIALOG_H
#include "ui_dialog.h"
//#include <QDialog>
namespace Ui {
class Dialog;
}
class Dialog : public QDialog, public Ui::Dialog
{
Q_OBJECT
public:
Dialog();
private slots:
void on_btnSend_clicked();
private:
Ui::Dialog *ui;
};
#endif // DIALOG_H
To copy to clipboard, switch view to plain text mode
dialog.cpp
#include "dialog.h"
#include "ui_dialog.h"
#include "mainwindow.h"
Dialog::Dialog()
{
setupUi(this);
}
void Dialog::on_btnSend_clicked()
{
QString textstring
= this
->le_myTextString
->text
();
MainWindow::setMyTextString(textstring);
}
#include "dialog.h"
#include "ui_dialog.h"
#include "mainwindow.h"
Dialog::Dialog()
{
setupUi(this);
}
void Dialog::on_btnSend_clicked()
{
QString textstring = this->le_myTextString->text();
MainWindow::setMyTextString(textstring);
}
To copy to clipboard, switch view to plain text mode
Any hints appreciated!
Bookmarks