Results 1 to 6 of 6

Thread: Connecting textEdit with label

  1. #1
    Join Date
    Jun 2020
    Posts
    11
    Qt products
    Qt4 Qt5

    Default Connecting textEdit with label

    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

    Qt Code:
    1. void Volk_Auswahl::label_name()
    2. {
    3. connect(ui->textEdit_name, SIGNAL(textChanged()), this, SLOT(label_name()));
    4. }
    To copy to clipboard, switch view to plain text mode 

    There is no error but when I type something in textEdit there is no text in label.
    Last edited by ejoty; 25th June 2020 at 13:22.

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,521
    Thanked 278 Times in 273 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Connecting textEdit with label

    And what is doing slot label_name() ?

  3. #3
    Join Date
    Jun 2020
    Posts
    11
    Qt products
    Qt4 Qt5

    Default Re: Connecting textEdit with label

    I dont know which funtion to use. Sry I am new at this.

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,713
    Thanks
    259
    Thanked 759 Times in 749 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Connecting textEdit with label

    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.

    Qt Code:
    1. // Widget0 is the QWidget that contains the QTextEdit
    2. class Widget0 : public QWidget
    3. {
    4. Q_OBJECT;
    5.  
    6. public:
    7. Widget0( QWidget * parent = nullptr );
    8.  
    9. // ...
    10. signals:
    11. void nameChanged( const QString & name );
    12.  
    13. private slots:
    14. void onTextChanged();
    15.  
    16. private:
    17. Widget0::Ui ui; // Contains the QTextEdit or QLineEdit
    18. };
    19.  
    20. // In the constructor, set up a signal slot connection so the QTextEdit can tell Widget0
    21. // when its contents have changed
    22. Widget0::Widget0( QWidget * parent )
    23. : QWidget( parent )
    24. {
    25. ui.setupUi( this );
    26.  
    27. connect( ui.textEdit, SIGNAL( textChanged() ), this, SLOT( onTextChanged() ) );
    28. }
    29.  
    30. // Slot implementation. When the text has changed, this will be called.
    31. // In it, we get the contents of the QTextEdit as an ordinary string,
    32. // then emit our signal so the rest of the world can be notified
    33. void Widget0::onTextChanged()
    34. {
    35. QString text = ui->textEdit->toPlainText();
    36. emit nameChanged( text );
    37. }
    38.  
    39. // Widget1 is the QWidget that contains the QLabel
    40. class Widget1 : public QWidget
    41. {
    42. Q_OBJECT;
    43.  
    44. public:
    45. Widget1( QWidget * parent = nullptr );
    46. // ...
    47.  
    48. public slots:
    49. void onNameChanged( const QString & name );
    50.  
    51. private:
    52. Widget1::Ui ui; // contains the QLabel
    53. };
    54.  
    55. Widget1::Widget1( QWidget * parent )
    56. : QWidget( parent )
    57. {
    58. ui.setupUi( this );
    59. }
    60.  
    61. // Slot. Sets the label text
    62. void Widget1::onNameChanged( const QString & name )
    63. {
    64. ui.label_name->setText( name );
    65. }
    66.  
    67.  
    68. // In your MainWindow constructor is where you connect Widget0 and Widget1
    69. // together
    70.  
    71. MainWindow::MainWindow( QWidget * parent )
    72. : QMainWindow( parent )
    73. {
    74. ui.setupUi( this );
    75.  
    76. // The magic part. Now the QTextEdit's signal gets relayed through Widget0
    77. // to the slot in Widget1, which sets the label text.
    78. connect( ui.widget0, SIGNAL( nameChanged( const QString & ) ), ui.widget1, SLOT( onNameChanged( const QString & ) ) );
    79. }
    To copy to clipboard, switch view to plain text mode 
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  5. #5
    Join Date
    Jun 2020
    Posts
    11
    Qt products
    Qt4 Qt5

    Default Re: Connecting textEdit with label

    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:
    Qt Code:
    1. 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
    2. {
    3. Q_OBJECT; // I do not know
    4.  
    5. public:
    6. Widget0( QWidget * parent = nullptr ); //I think that we connect the adress of our object with a pointer but I am not sure
    7.  
    8.  
    9. signals:
    10. void nameChanged( const QString & name ); // I do not know
    11.  
    12. private slots:
    13. void onTextChanged(); // I do not know
    14.  
    15. private:
    16. 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"
    17. }
    18.  
    19.  
    20. Widget0::Widget0( QWidget * parent ) // I also do not know what it means when I write somesting with the two colons
    21. : QWidget( parent )
    22. {
    23. ui.setupUi( this ); // I do not know
    24.  
    25. connect( ui.textEdit, SIGNAL( textChanged() ), this, SLOT( onTextChanged() ) ); //connecting the textEdit when ist text is changed with a SLOT ?
    26. }
    27.  
    28.  
    29. void Widget0::onTextChanged() // I do not know
    30. {
    31. 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
    32. emit nameChanged( text ); //the text gets emitted so that my Slot can use it
    33. }
    34.  
    35. //more or less the same as with the Widget0
    36. class Widget1 : public QWidget
    37. {
    38. Q_OBJECT;
    39.  
    40. public:
    41. Widget1( QWidget * parent = nullptr );
    42. // ...
    43.  
    44. public slots:
    45. void onNameChanged( const QString & name );
    46.  
    47. private:
    48. Widget1::Ui ui; // contains the QLabel
    49. };
    50.  
    51. Widget1::Widget1( QWidget * parent )
    52. : QWidget( parent )
    53. {
    54. ui.setupUi( this );
    55. }
    56.  
    57.  
    58. void Widget1::onNameChanged( const QString & name )
    59. {
    60. ui.label_name->setText( name );
    61. }
    62.  
    63.  
    64.  
    65.  
    66. MainWindow::MainWindow( QWidget * parent ) //Do I Always use Mainwindow? Because I am in a QDialog window
    67. : QMainWindow( parent )
    68. {
    69. ui.setupUi( this );
    70.  
    71.  
    72. connect( ui.widget0, SIGNAL( nameChanged( const QString & ) ), ui.widget1, SLOT( onNameChanged( const QString & ) ) );
    73. }
    To copy to clipboard, switch view to plain text mode 

    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:
    Qt Code:
    1. void Window::on_lineEdit_cname_textChanged(const QString &arg1)
    2. {
    3. ui->label_cname->setText(arg1);
    4. }
    To copy to clipboard, switch view to plain text mode 

    But I would still be intersted in other Solutions.
    Last edited by ejoty; 26th June 2020 at 00:43.

  6. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,713
    Thanks
    259
    Thanked 759 Times in 749 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Connecting textEdit with label

    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. The examples for Qr Widgets are here. I would start with the Calculator example 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.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Replies: 0
    Last Post: 4th November 2016, 00:27
  2. Qml TextEdit
    By goli in forum Newbie
    Replies: 4
    Last Post: 28th April 2011, 15:37
  3. Inserting label inside textEdit widget
    By dshan in forum Qt Programming
    Replies: 1
    Last Post: 11th January 2011, 12:17
  4. Replies: 3
    Last Post: 13th May 2010, 00:53
  5. How to make text in label or TextEDit to flash?
    By qtlinuxnewbie in forum Newbie
    Replies: 2
    Last Post: 6th April 2010, 08:53

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.