Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. UI blocked when writing into textEdit

UI blocked when writing into textEdit

Scheduled Pinned Locked Moved Unsolved General and Desktop
14 Posts 5 Posters 3.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mourad_bilog
    wrote on last edited by VRonin
    #1

    I've an application witch update execute commands on a database. I've a function witch execute that commands and in the in this function I display some messages to the user.

    My problem is that the texts written into the textEdit are displayed at the end of the function work.

    I've tried to use QThread but it does'nt resolve the problem

    this->moveToThread(&threadText);
    connect(&threadText, SIGNAL(started()), this, SLOT(writeTexte()));
    threadText.start();
    

    Does anyone have an idea how to proceed to write into textEdit and not block the UI ?

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      You were close to the right solution. this should emit a signal and this signal (not the thread started) should be connected to a slot that then prints it to the textEdit

      Alternative and worst solution is to add QApplication::processEvents() to your existing function

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      2
      • M Offline
        M Offline
        mourad_bilog
        wrote on last edited by
        #3

        thanks for replying.

        I've tried the following code but the problem persists

        this->moveToThread(&threadText);
        connect(this, SIGNAL(writeIntoText()), textEdit, SLOT(append(QString))); // writeIntoText() is a signal that I'm created
        threadText.start();
        
        updateDatabase();
        

        Can you tell what I'm wrong

        1 Reply Last reply
        0
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #4

          writeIntoText() should have a qstring arguments that gets passed to the slot so it knows what to write

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          1 Reply Last reply
          1
          • M Offline
            M Offline
            mourad_bilog
            wrote on last edited by
            #5

            Thanks for replying,

            I've modified the slot signature but the ui is still blocked until the treatement end.

            How I must use the thread to unblock the UI when the function is updating database ?

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Hi,

              What is the object that contains threadText ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • M Offline
                M Offline
                mourad_bilog
                wrote on last edited by
                #7

                I would like to link the textEdit to the Thread but it's impossible with Qt (Message : "Can't move object having parent").

                Reading most documents, I found that the problem can be resolved by a simple signal / slot, It's true ? If yes, can you tell me what I'm wrong in this code :

                connect(this, SIGNAL(writeIntoText(QString)), textEdit, SLOT(WriteMessage(QString))); // writeIntoText(QString) and WriteMessage(QString) are created by me
                
                J.HilkJ jsulmJ 2 Replies Last reply
                0
                • M mourad_bilog

                  I would like to link the textEdit to the Thread but it's impossible with Qt (Message : "Can't move object having parent").

                  Reading most documents, I found that the problem can be resolved by a simple signal / slot, It's true ? If yes, can you tell me what I'm wrong in this code :

                  connect(this, SIGNAL(writeIntoText(QString)), textEdit, SLOT(WriteMessage(QString))); // writeIntoText(QString) and WriteMessage(QString) are created by me
                  
                  J.HilkJ Offline
                  J.HilkJ Offline
                  J.Hilk
                  Moderators
                  wrote on last edited by
                  #8

                  @mourad_bilog
                  hi,
                  I'm afraifd you're not giving us enough information here.

                  For example if your text/String Generating code is in the same Thread as your UI, the UI will be blocked regardless of yous SIGNAL/SLOT connection working or not.

                  on the otherhand, how is

                  textEdit
                  

                  created, is it actually an pointer to your QTextEdit or not. What is your compiler error if you have one, you're using the old syntax so you'll see the error messages only after running the program.

                  try to change it to the new syntax

                  connect(sender, &QObject::Signal, receiver, &QObject::Slot);
                  

                  it will not compile if you have an error and you can give us the error msg.


                  Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                  Q: What's that?
                  A: It's blue light.
                  Q: What does it do?
                  A: It turns blue.

                  1 Reply Last reply
                  0
                  • M mourad_bilog

                    I would like to link the textEdit to the Thread but it's impossible with Qt (Message : "Can't move object having parent").

                    Reading most documents, I found that the problem can be resolved by a simple signal / slot, It's true ? If yes, can you tell me what I'm wrong in this code :

                    connect(this, SIGNAL(writeIntoText(QString)), textEdit, SLOT(WriteMessage(QString))); // writeIntoText(QString) and WriteMessage(QString) are created by me
                    
                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @mourad_bilog You cannot use Qt GUI classes in other threads (only in main/GUI thread).
                    Yes, signal/slot should work.
                    Is textEdit a QTextEdit? If so then what you have will not work as there is no such slot in QTextEdit: WriteMessage
                    Why not append(QString)?

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by VRonin
                      #10

                      Your setup is currently not clear since it seems you have an object that contains a thread that you move to and that object might or might not be a widget.

                      The usual modus operandi is something like:
                      WARNING Code example, it doesn't mean that you have to do this specifically in the main function.

                      main.cpp:
                      
                      QApplication app(arg, argv);
                      MyWidget widget;
                      MyWorkerObject worker;
                      QThread thread;
                      worker.moveToThread(&thread);
                      connect(&worker, &MyWorkerObject::mySignal, &widget, &MyWidget::mySlot);
                      thread.start();
                      widget.show();
                      return app.exec();
                      

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      J.HilkJ 1 Reply Last reply
                      4
                      • SGaistS SGaist

                        Your setup is currently not clear since it seems you have an object that contains a thread that you move to and that object might or might not be a widget.

                        The usual modus operandi is something like:
                        WARNING Code example, it doesn't mean that you have to do this specifically in the main function.

                        main.cpp:
                        
                        QApplication app(arg, argv);
                        MyWidget widget;
                        MyWorkerObject worker;
                        QThread thread;
                        worker.moveToThread(&thread);
                        connect(&worker, &MyWorkerObject::mySignal, &widget, &MyWidget::mySlot);
                        thread.start();
                        widget.show();
                        return app.exec();
                        
                        J.HilkJ Offline
                        J.HilkJ Offline
                        J.Hilk
                        Moderators
                        wrote on last edited by VRonin
                        #11

                        @SGaist
                        you should fix the typo, object to worker or its a bit confusing.


                        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                        Q: What's that?
                        A: It's blue light.
                        Q: What does it do?
                        A: It turns blue.

                        1 Reply Last reply
                        0
                        • M Offline
                          M Offline
                          mourad_bilog
                          wrote on last edited by
                          #12

                          @J-Hilk, Thanks for your remarks. Effectively, messages inserted into the TextEdit are in the same Thread used by the UI.

                          In fact, I've not errors displayed. Juste the UI is blocked during the treatement of database update and all the message are diplayed at the end when function is totaly executed.

                          textEdit is an object QTextEdit created by the Qt Creator.

                          @jsulm Thanks for replying. textEdit is a QTextEdit Object. I've used the append signal but I've the following error :

                          QObject::connect: No such signal QTextEdit::writeMessage(QString) in TheriaUpdater.cpp:866
                          QObject::connect:  (sender name:   'textEdit')
                          QObject::connect:  (receiver name: 'frmTheriaUpdater')
                          
                          J.HilkJ 1 Reply Last reply
                          0
                          • SGaistS Offline
                            SGaistS Offline
                            SGaist
                            Lifetime Qt Champion
                            wrote on last edited by
                            #13

                            As the error message state: you are trying to connect a signal that doesn't exist in QTextEdit.

                            Interested in AI ? www.idiap.ch
                            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                            1 Reply Last reply
                            1
                            • M mourad_bilog

                              @J-Hilk, Thanks for your remarks. Effectively, messages inserted into the TextEdit are in the same Thread used by the UI.

                              In fact, I've not errors displayed. Juste the UI is blocked during the treatement of database update and all the message are diplayed at the end when function is totaly executed.

                              textEdit is an object QTextEdit created by the Qt Creator.

                              @jsulm Thanks for replying. textEdit is a QTextEdit Object. I've used the append signal but I've the following error :

                              QObject::connect: No such signal QTextEdit::writeMessage(QString) in TheriaUpdater.cpp:866
                              QObject::connect:  (sender name:   'textEdit')
                              QObject::connect:  (receiver name: 'frmTheriaUpdater')
                              
                              J.HilkJ Offline
                              J.HilkJ Offline
                              J.Hilk
                              Moderators
                              wrote on last edited by
                              #14

                              @mourad_bilog

                              ok, the correct way to connect your generated QString to be displayed in the textedit should be this: (assuming your class is called MainWindow)

                              //to Replace old text
                              connect(this, &MainWindow::writeIntoText, ui->textEdit, QTextEdit::setText);
                              //to append new String to the old text
                              connect(this, &MainWindow::writeIntoText, ui->textEdit, QTextEdit::append);
                              

                              But as long as your function, that "emits writeIntoText" is not wraped into a QThread or QtConcurrent, the gui will most likly still be frozen.


                              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                              Q: What's that?
                              A: It's blue light.
                              Q: What does it do?
                              A: It turns blue.

                              1 Reply Last reply
                              0

                              • Login

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • Users
                              • Groups
                              • Search
                              • Get Qt Extensions
                              • Unsolved