Send a text to textEdit from a non qt cpp file
-
@saeid0034
Glancing through your code, it looks reasonable. As ever, while developing useqDebug()messages to verify your signal is being emitted, received etc.@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()
SendMSGfunction 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. -
@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()
SendMSGfunction 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.@saeid0034
Look, I'm not sure [actually, I am sure], but: inCreateRemoteThread_Type1you have aCreateRemoteThread_c Sender;local variable which is the signal sender. Meanwhile in theconnect()you have a newCreateRemoteThread_cobject,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?? -
@saeid0034
Look, I'm not sure [actually, I am sure], but: inCreateRemoteThread_Type1you have aCreateRemoteThread_c Sender;local variable which is the signal sender. Meanwhile in theconnect()you have a newCreateRemoteThread_cobject,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?? -
@JonB
oh, yes you right, but one question how can I use one object for bothmainwindowandCreateRemoteThread_Type1?@saeid0034
I knew you were going to ask that next! :) I'm afraid, that's your problem! I don't know enough about how yourCreateRemoteThread_ccode/object(s) fit together with your main window code. Do you createCreateRemoteThread_cinstances 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 functionCreateRemoteThread_Type1is about, why you needed that.Only you know how your code relates. Whatever, the instance that does the
emitof the signal must be the same instance as main window specifies in itsconnect()statement, that is what is vital. -
@saeid0034
I knew you were going to ask that next! :) I'm afraid, that's your problem! I don't know enough about how yourCreateRemoteThread_ccode/object(s) fit together with your main window code. Do you createCreateRemoteThread_cinstances 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 functionCreateRemoteThread_Type1is about, why you needed that.Only you know how your code relates. Whatever, the instance that does the
emitof the signal must be the same instance as main window specifies in itsconnect()statement, that is what is vital.@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 yourCreateRemoteThread_ccode/object(s) fit together with your main window code. Do you createCreateRemoteThread_cinstances 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 functionCreateRemoteThread_Type1is about, why you needed that.Only you know how your code relates. Whatever, the instance that does the
emitof the signal must be the same instance as main window specifies in itsconnect()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 fromCreateRemoteThread_Type1andSetPrivilegein my mainwindow code its okay? -
@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 yourCreateRemoteThread_ccode/object(s) fit together with your main window code. Do you createCreateRemoteThread_cinstances 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 functionCreateRemoteThread_Type1is about, why you needed that.Only you know how your code relates. Whatever, the instance that does the
emitof the signal must be the same instance as main window specifies in itsconnect()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 fromCreateRemoteThread_Type1andSetPrivilegein my mainwindow code its okay?@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.
-
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 -
@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.
@JonB
Hi, I worked with it a little more and I come up with this codesworker.hclass 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.cppbool Worker::CreateRemoteThread_Type1(LPCSTR ID, HANDLE hProcess) { //some code emit updateUI(0, "text", Null); }mainwindow.hprivate slots: void Reciver(const int error, const QString text, const QString value);and
mainwindow.cppWorker 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