Seite 1 von 1

QDialog: Wie string an Hauptfenster zurückgeben?

Verfasst: 26. November 2008 13:51
von jp
Hallo,

ich bin noch ganz neu mit Qt. Ich wollte mir als erstes Projekt ein kleines Telefonbuch programmieren. Aber ich komme nicht weiter, weil ich nicht weiß, wie man einen (Q)String an das Hauptfenster vom Dialogfeld zurückgibt. Wie man einen Integer zurückgibt, weiß ich, das macht man den Slots done( int r ) oder finished( int r ).
Mein Ansatz dazu ist:

frame.h

Code: Alles auswählen

#ifndef FRAME_H
#define FRAME_H
#include <QWidget>
#include <QPushButton>
#include <QLabel>
#include <QVBoxLayout>
#include "dialog.h"

class Frame : public QWidget {
    Q_OBJECT
    public:
           Frame( QWidget *parent = 0 );
    public slots:
           void textausgabe();
    private:
            QPushButton *button;          
            QLabel *label;
            QVBoxLayout *layout;
            Dialog *dialog;
};
#endif
frame.cpp

Code: Alles auswählen

#include "frame.h"

Frame::Frame( QWidget *parent ) {
         
    button = new QPushButton( "Dialog öffnen" );
    label  = new QLabel( "Textausgabe" );
    layout = new QVBoxLayout( this );
    dialog = new Dialog;
    layout->addWidget( button );
    layout->addWidget( label );             
    
    connect( button, SIGNAL( clicked() ),
             dialog, SLOT( exec() ) );
    connect( dialog, SIGNAL( accepted() ),
             this, SLOT( textausgabe() ) );  
}              

void Frame::textausgabe() {

    int retVal = dialog->result();
    if( retVal == QDialog::Accepted ) {
        label->setText( dialog->getStr() );    
    }        
}
dialog.h

Code: Alles auswählen

#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
#include <QPushButton>
#include <QLineEdit>
#include <QVBoxLayout>
#include <QString>

class Dialog : public QDialog {
    Q_OBJECT
    public:
           Dialog();
           QString getStr() {
               return str;        
           }
            void getTextfeldStr( QString s );           
    public slots:
            void setStr( QString s ) {
                 str = s;     
            }          
    private:
            QVBoxLayout *d_layout;      
            QPushButton *d_button;
            QLineEdit *d_textfeld;
            QString str;

};
#endif

dialog.cpp

Code: Alles auswählen

#include "dialog.h"

Dialog::Dialog() {
                 
     d_textfeld = new QLineEdit;
     d_button   = new QPushButton( "OK" );
     d_layout   = new QVBoxLayout;
     
     d_layout->addWidget( d_textfeld );
     d_layout->addWidget( d_button );
     setLayout( d_layout );
     
     connect( d_button, SIGNAL( clicked() ),
              this, SLOT( accept() ) );               
                 
} 

void Dialog::getTextfeldStr( QString s ) {
    str = d_textfeld->text();    
}                
Im Label vom Hauptfenster soll dann der Text erscheinen, der im QLineEdit vom Dialogfeld eingegeben wurde. Kann mir da jemand weiterhelfen?

Verfasst: 26. November 2008 14:07
von pfid
getTextfeldStr löschen und in getStr()

Code: Alles auswählen

QString getStr() { returnd_textfeld->text(); }

Verfasst: 26. November 2008 14:13
von jp
Das ging schnell. Vielen Dank!