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. Send a text to textEdit from a non qt cpp file
Qt 6.11 is out! See what's new in the release blog

Send a text to textEdit from a non qt cpp file

Scheduled Pinned Locked Moved Solved General and Desktop
16 Posts 3 Posters 1.1k Views
  • 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.
  • JonBJ JonB

    @saeid0034
    Glancing through your code, it looks reasonable. As ever, while developing use qDebug() messages to verify your signal is being emitted, received etc.

    S Offline
    S Offline
    saeid0034
    wrote on last edited by saeid0034
    #7

    @JonB said in Send a text to textEdit from a non qt cpp file:

    Glancing through your code, it looks reasonable.

    as you said I check the code with qDebug()
    SendMSG function called but seems like signal doesn't emit
    I dont know what is my problem
    edit
    yes, I tested connect with lambda and tried to show a massage when signal received, but nothing happened.

    JonBJ 1 Reply Last reply
    0
    • S saeid0034

      @JonB said in Send a text to textEdit from a non qt cpp file:

      Glancing through your code, it looks reasonable.

      as you said I check the code with qDebug()
      SendMSG function called but seems like signal doesn't emit
      I dont know what is my problem
      edit
      yes, I tested connect with lambda and tried to show a massage when signal received, but nothing happened.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #8

      @saeid0034
      Look, I'm not sure [actually, I am sure], but: in CreateRemoteThread_Type1 you have a CreateRemoteThread_c Sender; local variable which is the signal sender. Meanwhile in the connect() you have a new CreateRemoteThread_c object, MsgSender. What is going on here? The connection must be from the object which sends the signal, and I think you have two completely separate objects here??

      S 1 Reply Last reply
      1
      • JonBJ JonB

        @saeid0034
        Look, I'm not sure [actually, I am sure], but: in CreateRemoteThread_Type1 you have a CreateRemoteThread_c Sender; local variable which is the signal sender. Meanwhile in the connect() you have a new CreateRemoteThread_c object, MsgSender. What is going on here? The connection must be from the object which sends the signal, and I think you have two completely separate objects here??

        S Offline
        S Offline
        saeid0034
        wrote on last edited by saeid0034
        #9

        @JonB
        oh, yes you right, but one question how can I use one object for both mainwindow and CreateRemoteThread_Type1?

        JonBJ 1 Reply Last reply
        0
        • S saeid0034

          @JonB
          oh, yes you right, but one question how can I use one object for both mainwindow and CreateRemoteThread_Type1?

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #10

          @saeid0034
          I knew you were going to ask that next! :) I'm afraid, that's your problem! I don't know enough about how your CreateRemoteThread_c code/object(s) fit together with your main window code. Do you create CreateRemoteThread_c instances more than once? If there really is only one you could use a singleton pattern. Or, main window can create it, I don't know what your non-class function CreateRemoteThread_Type1 is about, why you needed that.

          Only you know how your code relates. Whatever, the instance that does the emit of the signal must be the same instance as main window specifies in its connect() statement, that is what is vital.

          S 1 Reply Last reply
          1
          • JonBJ JonB

            @saeid0034
            I knew you were going to ask that next! :) I'm afraid, that's your problem! I don't know enough about how your CreateRemoteThread_c code/object(s) fit together with your main window code. Do you create CreateRemoteThread_c instances more than once? If there really is only one you could use a singleton pattern. Or, main window can create it, I don't know what your non-class function CreateRemoteThread_Type1 is about, why you needed that.

            Only you know how your code relates. Whatever, the instance that does the emit of the signal must be the same instance as main window specifies in its connect() statement, that is what is vital.

            S Offline
            S Offline
            saeid0034
            wrote on last edited by
            #11

            @JonB said in Send a text to textEdit from a non qt cpp file:

            @saeid0034
            I knew you were going to ask that next! :) I'm afraid, that's your problem! I don't know enough about how your CreateRemoteThread_c code/object(s) fit together with your main window code. Do you create CreateRemoteThread_c instances more than once? If there really is only one you could use a singleton pattern. Or, main window can create it, I don't know what your non-class function CreateRemoteThread_Type1 is about, why you needed that.

            Only you know how your code relates. Whatever, the instance that does the emit of the signal must be the same instance as main window specifies in its connect() statement, that is what is vital.

            thank you for all your help and Sorry for bother you so much
            at the end I think my best solution is change my code, one last question😅
            you think if I include all my needed function from CreateRemoteThread_Type1 and SetPrivilege in my mainwindow code its okay?

            JonBJ 1 Reply Last reply
            0
            • S saeid0034

              @JonB said in Send a text to textEdit from a non qt cpp file:

              @saeid0034
              I knew you were going to ask that next! :) I'm afraid, that's your problem! I don't know enough about how your CreateRemoteThread_c code/object(s) fit together with your main window code. Do you create CreateRemoteThread_c instances more than once? If there really is only one you could use a singleton pattern. Or, main window can create it, I don't know what your non-class function CreateRemoteThread_Type1 is about, why you needed that.

              Only you know how your code relates. Whatever, the instance that does the emit of the signal must be the same instance as main window specifies in its connect() statement, that is what is vital.

              thank you for all your help and Sorry for bother you so much
              at the end I think my best solution is change my code, one last question😅
              you think if I include all my needed function from CreateRemoteThread_Type1 and SetPrivilege in my mainwindow code its okay?

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #12

              @saeid0034 said in Send a text to textEdit from a non qt cpp file:

              you think if I include all my needed function from CreateRemoteThread_Type1 and SetPrivilege in my mainwindow code its okay?

              This is not ideal! You really want to keep separation between a Qt main window/program and some piece of Windows-specific code to do goodness what. Try to make a self-contained class out of what you have with this code. But this is a general programming issue.

              S 1 Reply Last reply
              1
              • JoeCFDJ Offline
                JoeCFDJ Offline
                JoeCFD
                wrote on last edited by JoeCFD
                #13

                send out your text via standard output std::cout in your cpp code and catch it in Qt code.
                https://forum.qt.io/topic/80024/how-to-redirect-output-of-stdout-to-gui-qtextedit-shell-from-one-process-to-another-process/2

                1 Reply Last reply
                1
                • S Offline
                  S Offline
                  saeid0034
                  wrote on last edited by saeid0034
                  #14

                  I manage to fix it wait add in function deceleration from worker.h to mainwindow.h
                  know I can send signal and receive it
                  I'm still don't know if its a good way to do this...

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    saeid0034
                    wrote on last edited by
                    #15

                    one other thing
                    know I can access ui from all c++ file I declared in header
                    its bad? and why?

                    1 Reply Last reply
                    0
                    • JonBJ JonB

                      @saeid0034 said in Send a text to textEdit from a non qt cpp file:

                      you think if I include all my needed function from CreateRemoteThread_Type1 and SetPrivilege in my mainwindow code its okay?

                      This is not ideal! You really want to keep separation between a Qt main window/program and some piece of Windows-specific code to do goodness what. Try to make a self-contained class out of what you have with this code. But this is a general programming issue.

                      S Offline
                      S Offline
                      saeid0034
                      wrote on last edited by saeid0034
                      #16

                      @JonB
                      Hi, I worked with it a little more and I come up with this codes

                      worker.h

                      class Worker : public QObject
                      {
                      	Q_OBJECT
                      
                      public:
                      	bool GetOSInfo();
                      	bool SetPrivilege(HANDLE hToken, LPCTSTR Privilege, BOOL bEnablePrivilege);
                      	void DisplayError(LPCSTR szAPI);
                      	int EscalatePrivilege();
                      	bool CreateRemoteThread_Type1(LPCSTR ID, HANDLE hProcess);
                      
                      signals:
                      	void updateUI(const int error, const QString text, const QString value);
                      
                      };
                      

                      CreateRemoteThread.cpp

                      bool Worker::CreateRemoteThread_Type1(LPCSTR ID, HANDLE hProcess) {
                      //some code
                      	emit updateUI(0, "text", Null);
                      }
                      

                      mainwindow.h

                      private slots:
                          void Reciver(const int error, const QString text, const QString value);
                      

                      and mainwindow.cpp

                      Worker worker; //global
                      
                      Mainwindow::Mainwindow(QWidget *parent)
                          : QMainWindow(parent)
                      {
                          ui.setupUi(this);
                      		
                      	QObject::connect(&worker, &Worker::updateUI, this, &mainwindow::Reciver);
                      }
                      
                      //some code
                      
                      void mainwindow::Reciver(const int error, const QString text, const QString value)
                      {
                      	ui.textEdit->append(text);
                      }
                      

                      its working and slot Trigger when signal fired
                      I only want to know there is any better way then declare Worker globally in mainwindow? (i declare it globally because I need it some other function other then main)
                      and one other thing, after all this code ideal?

                      thanks for all your helps and sorry for bothering you so many times

                      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