PDA

View Full Version : Connecting textEdit with label



ejoty
25th June 2020, 12:14
I tried to connect a textEdit to a label on a different page using stackedWidget, so that when a text is written in textEdit the same text is shown in the label on the next page, but so far it didnt work out. My textEdits Name is "textEdit_name" and my labels Name is "label_name". My Code so far is



void Volk_Auswahl::label_name()
{
connect(ui->textEdit_name, SIGNAL(textChanged()), this, SLOT(label_name()));
}


There is no error but when I type something in textEdit there is no text in label.

Lesiok
25th June 2020, 14:23
And what is doing slot label_name() ?

ejoty
25th June 2020, 14:44
I dont know which funtion to use. Sry I am new at this.

d_stranz
25th June 2020, 20:20
Showing 4 lines of code is not going to help us understand how to help you with your problem.

If this is basically the same problem as your other post, then please don't post the same question twice.

So I assume you have two QWidget-based classes that are the two widgets you have created for your two stack widget pages. One of these has a QTextEdit (or a QLineEdit - which you should be using if all you want is for the user to be able to enter a short string, like a name) and the other widget has a QLabel. You want the QLabel to display the same text that the user types into the QTextEdit or QLineEdit, right?

The code you posted above makes no sense at all, or maybe you don't really understand signals and slots. The connect() statement is basically saying, "When the first widget emits this signal, I want you to send it to this slot of the second widget". It prepares the two widgets for something that will happen at some time in the future. It is not an instruction to do something now. So putting a connect() statement inside a slot doesn't cause anything to happen (except add another connection each time that slot is called - probably not what you want).

The correct procedure, and one that creates an abstraction so that the widget on stack position 0 doesn't have to know anything about the contents of the widget on stack position 1 is to implement a signal and slot pair that can relay the signal from the QTextEdit / QLineEdit to the QLabel, through their parent widgets.

Something like this. Don't copy and paste this code. Study it and understand what it is doing.



// 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 & ) ) );
}

ejoty
25th June 2020, 23:43
I see that you value me learning what I do very much and I also want to understand it. It is very kind of you that you take the effort to explain it to me. I am going to be honest with you and hope that you will still help me. I am very new to c++ and Qt so most of the Code I do not understand so I would like to go through the Code and tell you what I think what line does. If you would be so kind to explain it to me I would be so grateful to you. If you would like to connect on a different platform than qtcentre I would be open to that as well. So here is what I think the Code does do:




class Widget0 : public QWidget //We declare a class named Widget0, QWidget is an object in that class and it is accessable out of that class since it is public
{
Q_OBJECT; // I do not know

public:
Widget0( QWidget * parent = nullptr ); //I think that we connect the adress of our object with a pointer but I am not sure


signals:
void nameChanged( const QString & name ); // I do not know

private slots:
void onTextChanged(); // I do not know

private:
Widget0::Ui ui; // I see ui everywhere but I do not know what it means to be honest. Also I get the error "no type named "Ui" in Widget0"
}


Widget0::Widget0( QWidget * parent ) // I also do not know what it means when I write somesting with the two colons
: QWidget( parent )
{
ui.setupUi( this ); // I do not know

connect( ui.textEdit, SIGNAL( textChanged() ), this, SLOT( onTextChanged() ) ); //connecting the textEdit when ist text is changed with a SLOT ?
}


void Widget0::onTextChanged() // I do not know
{
QString text = ui->textEdit->toPlainText(); // Not sure but Maybe the text that we wrote is called when we use the textEdit? I also do not know what toPlainText() does do
emit nameChanged( text ); //the text gets emitted so that my Slot can use it
}

//more or less the same as with the Widget0
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 );
}


void Widget1::onNameChanged( const QString & name )
{
ui.label_name->setText( name );
}




MainWindow::MainWindow( QWidget * parent ) //Do I Always use Mainwindow? Because I am in a QDialog window
: QMainWindow( parent )
{
ui.setupUi( this );


connect( ui.widget0, SIGNAL( nameChanged( const QString & ) ), ui.widget1, SLOT( onNameChanged( const QString & ) ) );
}



You do not have to explain everything to me I know that would be a lot of work. Maybe you can recommend a tutorial or for every bit of Information I would be thankful. Also, do I write the whole Code in my cpp file or do I declare the classes in my Header file? If you dont want to explain any further it would be nice to let me know so that I do not wait for an answer.

Added after 20 minutes:

Okay it was as simple as that:



void Window::on_lineEdit_cname_textChanged(const QString &arg1)
{
ui->label_cname->setText(arg1);
}



But I would still be intersted in other Solutions.

d_stranz
26th June 2020, 16:34
But I would still be intersted in other Solutions.

You really need to work through the basic tutorials that come with your Qt distribution. The comments you made are so crucial to how Qt works that the only way to really understand them is to work through the tutorials.

The main Qt tutorials page is here. (https://doc.qt.io/qt-5/qtexamplesandtutorials.html) The examples for Qr Widgets are here. (https://doc.qt.io/qt-5/examples-widgets.html) I would start with the Calculator example (https://doc.qt.io/qt-5/qtwidgets-widgets-calculator-example.html) because it is simple, but it also exmplains how to use layouts and how to connect signals and slots.

I would recommend that you get a copy of a good book on programming Qt in C++. One of the best I have read is C++ GUI Programming with Qt 4 (2nd Edition) by Jasmin Blanchette and Mark Summerfield. Even though it is based on Qt 4, there are very few differences as far as ordinary Qt development between Qt 4 and Qt 5.

An other good book is Game Programming Using Qt by Witold Wysota and Lorenz Haas. Even though the title says "game programming", it is still mostly a fundamental Qt book that uses the development of a game as the way to present more advanced topics. There are many other Qt books, but these are probably the most useful for beginners. You can buy both of them used on Amazon for not much money, or maybe you can find them in a library.