Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to use QTextEdit



  • I placed QTextEdit on a form.
    I see it in ui->setupUi(this);

    textEditTerminalRx = new QTextEdit(tab_terminal);
    textEditTerminalRx->setObjectName(QString::fromUtf8("textEditTerminalRx"));
    textEditTerminalRx->setGeometry(QRect(80, 100, 521, 91));
    

    How can I set text in textEditTerminalRx? what visibility scope? it's not seen at mainwindow.cpp.



  • @jenya7

    How can I set text in textEditTerminalRx?

    Try looking at the docs where it shows QTextEdit::setText().



  • @JonB said in How to use QTextEdit:

    @jenya7

    How can I set text in textEditTerminalRx?

    Try looking at the docs where it shows QTextEdit::setText().

    I can do

     QTextEdit txt_edit;
     txt_edit.setText("text");
    

    But how can I get a grip on my QTextEdit - textEditTerminalRx?



  • @jenya7
    You used to have

    textEditTerminalRx = new QTextEdit(tab_terminal);
    

    which at least gave it a parent, so it appeared on whatever tab_terminal is. Now that you have simply gone

     QTextEdit txt_edit;
     txt_edit.setText("text");
    

    the text edit won't even appear anywhere, without further code at least.

    But how can I get a grip on my QTextEdit - textEditTerminalRx?

    I realize English may not be your first language, but I really don't know what this means? Can you try to explain what it is you are trying to achieve such that we can help?



  • @jenya7
    Whe you design your form with the designer, an ui element is created in your widget's code.

    So calling ui->textEditTerminalRx should give you access to your text widget



  • @JonB
    Well... I want to set some text in me control (textEditTerminalRx). And I want to do it from some modules (cpp files)


  • Lifetime Qt Champion

    @jenya7 So, what stops you from doing:

    textEditTerminalRx->setText("MY TEXT");
    

    ?



  • @Gojir4 said in How to use QTextEdit:

    @jenya7
    Whe you design your form with the designer, an ui element is created in your widget's code.

    So calling ui->textEditTerminalRx should give you access to your text widget

    It works in mainwindow.cpp. But ui->textEditTerminalRx is not visible in another files.



  • @jsulm said in How to use QTextEdit:

    @jenya7 So, what stops you from doing:

    textEditTerminalRx->setText("MY TEXT");
    

    ?

    without ui-> is not visible.


  • Lifetime Qt Champion

    @jenya7 said in How to use QTextEdit:

    But ui->textEditTerminalRx is not visible in another files

    And it should not.

    You don't provide enough information to help you.
    Did you use designer or not?



  • @jenya7 Of course. It is only visible in MainWindow.
    You need to add a method (or slot) in your MainWindow allowing to access the control, or to set the text

    void MainWindow::setTerminalText(const QString &txt){
        ui->textEditTerminalRx->setText(txt);
    }
    


  • @jsulm said in How to use QTextEdit:

    @jenya7 said in How to use QTextEdit:

    But ui->textEditTerminalRx is not visible in another files

    And it should not.

    You don't provide enough information to help you.
    Did you use designer or not?

    Sorry but it is specified on the two first lines.

    @jenya7 said in How to use QTextEdit:

    I placed QTextEdit on a form.
    I see it in ui->setupUi(this);



  • @Gojir4 said in How to use QTextEdit:

    @jenya7 Of course. It is only visible in MainWindow.
    You need to add a method (or slot) in your MainWindow allowing to access the control, or to set the text

    void MainWindow::setTerminalText(const QString &txt){
        ui->textEditTerminalRx->setText(txt);
    }
    

    Can I invoke this method from another file? say in sys.cpp I have

    void SYS_SomeFunc ()
    {
        DoSomeTask();
    
         DisplayTextIn_textEditTerminalRx();
    }
    

    Well...It demands some further explanation. I want to provide to a user some indication what’s going on in the project’s running tasks. So instead of flooding him with numerous pop-up QMessageBox I want to print out the messages at my textEditTerminalRx.



  • @Gojir4 said in How to use QTextEdit:

    @jenya7 Of course. It is only visible in MainWindow.
    You need to add a method (or slot) in your MainWindow allowing to access the control, or to set the text
    void MainWindow::setTerminalText(const QString &txt){
    ui->textEditTerminalRx->setText(txt);
    }

    OOI, how does this help? How does a different class/module/form get any access to the MainWindow instance to call that method, any more than to the ui?



  • @jenya7 said in How to use QTextEdit:

    Well...It demands some further explanation. I want to provide to a user some indication what’s going on in the project’s running tasks. So instead of flooding him with numerous pop-up QMessageBox I want to print out the messages at my textEditTerminalRx.

    You either need to decide to gain access to the main window's text edit so that you can set its text directly, or (perhaps more suitable for your use case) you could have it raise a signal when stuff is"going on", and the main window could place a slot on that signal so that it updates the text edit.



  • @JonB said in How to use QTextEdit:

    @jenya7 said in How to use QTextEdit:

    Well...It demands some further explanation. I want to provide to a user some indication what’s going on in the project’s running tasks. So instead of flooding him with numerous pop-up QMessageBox I want to print out the messages at my textEditTerminalRx.

    You either need to decide to gain access to the main window's text edit so that you can set its text directly, or (perhaps more suitable for your use case) you could have it raise a signal when stuff is"going on", and the main window could place a slot on that signal so that it updates the text edit.

    Looks like fun. How do I do that?



  • @jenya7
    You start by reading https://doc.qt.io/qt-5/signalsandslots.html, or similar, which explains Qt's signals & slots. You're going to need that anyway to get anywhere in Qt, so it's not time wasted!

    Then in principle you declare a signal out of your other file where you are "doing stuff which must be reported", include that header file into main window so it knows about the signal, and have main window put a slot on that signal. The slot does the setText(). The signalling class can pass any necessary arguments (e..g what to write, how far it's got...).

    Your "project’s running tasks" sound like you want them to run in a thread, so that they do not block the UI? And they would send these signals from their thread to be acted on in the main window UI thread.



  • Can I create signals and slots withing a class only? Can I do it in a file like

    my_file.cpp:

    #include ''my_file.h''

    slot my_slot;
    signal my_signal;

    void Func(void)
    {
    }

    //end of file


  • Lifetime Qt Champion

    @jenya7
    Hi
    Yes its only with classes.
    And it needs to derive from QObject and have the Q_OBJECT macro and be in a .h file.

    class MyClass : public QObject
    {
    Q_OBJECT
    ....



  • Thank you.



  • @JonB said in How to use QTextEdit:

    @Gojir4 said in How to use QTextEdit:

    @jenya7 Of course. It is only visible in MainWindow.
    You need to add a method (or slot) in your MainWindow allowing to access the control, or to set the text
    void MainWindow::setTerminalText(const QString &txt){
    ui->textEditTerminalRx->setText(txt);
    }

    OOI, how does this help? How does a different class/module/form get any access to the MainWindow instance to call that method, any more than to the ui?

    @JonB said in How to use QTextEdit:

    You either need to decide to gain access to the main window's text edit so that you can set its text directly, or (perhaps more suitable for your use case) you could have it raise a signal when stuff is"going on", and the main window could place a slot on that signal so that it updates the text edit.

    :P thanks for this moment of fun



  • @Gojir4 said in How to use QTextEdit:

    :P thanks for this moment of fun

    ??


Log in to reply