// Widget0 is the QWidget that contains the QTextEdit
{
Q_OBJECT;
public:
Widget0
( QWidget * parent
= nullptr
);
// ...
signals:
void nameChanged
( const QString & name
);
private slots:
void onTextChanged();
private:
Widget0::Ui ui; // Contains the QTextEdit or QLineEdit
};
// In the constructor, set up a signal slot connection so the QTextEdit can tell Widget0
// when its contents have changed
Widget0
::Widget0( QWidget * parent
){
ui.setupUi( this );
connect( ui.textEdit, SIGNAL( textChanged() ), this, SLOT( onTextChanged() ) );
}
// Slot implementation. When the text has changed, this will be called.
// In it, we get the contents of the QTextEdit as an ordinary string,
// then emit our signal so the rest of the world can be notified
void Widget0::onTextChanged()
{
QString text
= ui
->textEdit
->toPlainText
();
emit nameChanged( text );
}
// Widget1 is the QWidget that contains the QLabel
{
Q_OBJECT;
public:
Widget1
( QWidget * parent
= nullptr
);
// ...
public slots:
void onNameChanged
( const QString & name
);
private:
Widget1::Ui ui; // contains the QLabel
};
Widget1
::Widget1( QWidget * parent
){
ui.setupUi( this );
}
// Slot. Sets the label text
void Widget1
::onNameChanged( const QString & name
) {
ui.label_name->setText( name );
}
// In your MainWindow constructor is where you connect Widget0 and Widget1
// together
MainWindow
::MainWindow( QWidget * parent
){
ui.setupUi( this );
// The magic part. Now the QTextEdit's signal gets relayed through Widget0
// to the slot in Widget1, which sets the label text.
connect( ui.
widget0,
SIGNAL( nameChanged
( const QString & ) ), ui.
widget1,
SLOT( onNameChanged
( const QString & ) ) );
}
// Widget0 is the QWidget that contains the QTextEdit
class Widget0 : public QWidget
{
Q_OBJECT;
public:
Widget0( QWidget * parent = nullptr );
// ...
signals:
void nameChanged( const QString & name );
private slots:
void onTextChanged();
private:
Widget0::Ui ui; // Contains the QTextEdit or QLineEdit
};
// In the constructor, set up a signal slot connection so the QTextEdit can tell Widget0
// when its contents have changed
Widget0::Widget0( QWidget * parent )
: QWidget( parent )
{
ui.setupUi( this );
connect( ui.textEdit, SIGNAL( textChanged() ), this, SLOT( onTextChanged() ) );
}
// Slot implementation. When the text has changed, this will be called.
// In it, we get the contents of the QTextEdit as an ordinary string,
// then emit our signal so the rest of the world can be notified
void Widget0::onTextChanged()
{
QString text = ui->textEdit->toPlainText();
emit nameChanged( text );
}
// Widget1 is the QWidget that contains the QLabel
class Widget1 : public QWidget
{
Q_OBJECT;
public:
Widget1( QWidget * parent = nullptr );
// ...
public slots:
void onNameChanged( const QString & name );
private:
Widget1::Ui ui; // contains the QLabel
};
Widget1::Widget1( QWidget * parent )
: QWidget( parent )
{
ui.setupUi( this );
}
// Slot. Sets the label text
void Widget1::onNameChanged( const QString & name )
{
ui.label_name->setText( name );
}
// In your MainWindow constructor is where you connect Widget0 and Widget1
// together
MainWindow::MainWindow( QWidget * parent )
: QMainWindow( parent )
{
ui.setupUi( this );
// The magic part. Now the QTextEdit's signal gets relayed through Widget0
// to the slot in Widget1, which sets the label text.
connect( ui.widget0, SIGNAL( nameChanged( const QString & ) ), ui.widget1, SLOT( onNameChanged( const QString & ) ) );
}
To copy to clipboard, switch view to plain text mode
Bookmarks