[CLOSED] String and classes



  • Hello, I have two classes

    Class 1 = MainWindow

    Class 2 = Dialog

    Dialog contains textedit and should store string into MainWindow... How can I do that???

    How it should looks like

    Class 1
    QString test;

    Class 2
    void Dialog::onButtonClicked{
    test = uiDialog->textEdit->text();
    }


  • Lifetime Qt Champion

    Hi,

    Add a getter to your dialog and retrieve the string after you have called exec on it

    I would recommend going through Qt's examples and demos. You'll find a lot of valuable information to get you started



  • I was trying to find example on Google but not very successfully...

    I used Refractor in Qt and it made Getter and Setter, but don't know what to do with it now...

    Dialog .h
    @#ifndef DIALOG_H
    #define DIALOG_H
    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QDialog>

    namespace Ui {
    class Dialog;
    }

    class Dialog : public QDialog
    {
    Q_OBJECT

    public:
    explicit Dialog(QMainWindow *parent = 0);
    ~Dialog();
    Ui::MainWindow *ui;
    QString string;

    QString getString() const;
    void setString(const QString &value);

    private slots:
    void on_button_clicked();

    private:
    Ui::Dialog *uis;
    };

    #endif // DIALOG_H

    @

    Dialog .cpp
    @#include "dialog.h"
    #include "ui_dialog.h"
    #include "ui_mainwindow.h"
    #include "mainwindow.h"

    Dialog::Dialog(QMainWindow *parent) :
    QDialog(parent),
    uis(new Ui::Dialog)
    {
    uis->setupUi(this);
    }

    Dialog::~Dialog()
    {
    delete uis;
    }
    QString Dialog::getString() const
    {
    return string;
    }

    void Dialog::setString(const QString &value)
    {
    string = value;
    }

    void Dialog::on_button_clicked()
    {

    }
    @


  • Lifetime Qt Champion

    Why would you need MainWindow::Ui in your dialog ?

    @
    QString Dialog::getString() const
    {
    return uis->myLineEdit->text();
    }

    void Dialog::setString(const QString &value)
    {
    uis->myLineEdit->setText(value);
    }@

    I would recommend reading Qt's documentation/tutorials/examples before going further. You'll get a better grasp on how things work



  • Oh I was testing something, thats why MainWindow::Ui is there...

    Where exactly do I have to read??? I went through few documentations but none of them explained use of getter/setter...



  • I figured out I can design QDialog even without designer pretty well, therefore I won't need these functions at this moment...

    Thanks anyways


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.