Does Anyone Know How To Link A Qt Button With A Terminal Command.....? :3

blackneos940

Active Member
Joined
May 16, 2017
Messages
347
Reaction score
207
Credits
332
I was wondering this today, and I'm not sure how..... :\ Keep in mind that I'm still pretty new to Qt, and might need a complete File, #includes, int main(), and all..... :3 I wanna do all this from Nano, so I don't have to use Qt Creator, if possible..... :3 Thank you SO much, guys..... :3 Man, it's really pouring here, or was..... Hard to tell..... :3

Edit: I should have clarified this earlier, but I'm trying to learn Qt without using a GUI (gotta' do this OLD school, the way Dennis Ritchie and Ken Thompson did..... :))
 
Last edited:


I haven't got time to find, or create an example ATM. I have a few things going on at work and at home. But I will come back to this in a few days!

But offhand, you'd need a button and a QThread object. The QThread object will have to be set up with the path to the command/executable it is to run. Then you set up the button to start the thread running when it is clicked.

Also, it is possible to create QT projects from the command line using qmake.
First you write your code.
Then you have to run qmake a couple of times (once to scan the source code for various dependencies and generate a Qt project file) The second will generate a makefile from the project file. Then you run make to build your executable.

Again, I'll get back to you with more detail when I can!
 
I haven't got time to find, or create an example ATM. I have a few things going on at work and at home. But I will come back to this in a few days!

But offhand, you'd need a button and a QThread object. The QThread object will have to be set up with the path to the command/executable it is to run. Then you set up the button to start the thread running when it is clicked.

Also, it is possible to create QT projects from the command line using qmake.
First you write your code.
Then you have to run qmake a couple of times (once to scan the source code for various dependencies and generate a Qt project file) The second will generate a makefile from the project file. Then you run make to build your executable.

Again, I'll get back to you with more detail when I can!
I've been away for a while..... Sorry, good sir..... :3 Anyway, here I am!..... :3
 
OK, I've managed to put a quick project together that uses QT and C++ to create a window with a button and a text box.
When you click the button, it fires off a hard-coded program in a separate process and displays its output in the text-box.

Unfortunately, this particular solution uses qtcreator. But if you are learning QT, then qtcreator is probably your best bet because of the built-in intellisense/code completion - which can help a lot when exploring QT's massive libraries.

I haven't been able to work out how to manually set up the widgets without having to use their form designer and it UI backend yet. I know I have some old QT projects that were written completely manually using QT3 and QT4, but I haven't been able to find them yet. :/

Anyway, I've got something that works and it's not a huge amount of code. I just need to start documenting what I did and then put that together into a tutorial/post here for you.

Hopefully only another few days!
 
Standard C++ would be Fork + exec, therefore in the button callback you should be able to call both functions as needed.
http://man7.org/linux/man-pages/man3/exec.3.html
http://man7.org/linux/man-pages/man2/fork.2.html

@ryanvade
Yes, in a standard C/C++ application you could use fork and exec, or popen in a callback to fire off another process, but @blackneos940 was asking about QT, so I'm trying to keep it strictly QT related. QT has several classes that help with controlling processes and multithreading etc.

@blackneos940:
As an update - I finally found an up to date resource in the QT5 documentation which describes how to create a QT GUI by hand - without using qtcreator or qtdesigner:
https://doc.qt.io/qt-5/qtwidgets-tutorials-addressbook-part1-example.html

The above page is part of an addressbook tutorial I found in the "layouts" section of the qt5 Widgets documentation.


For now, I'm only interested in the UI design section where they create the UI using code, without having to use 'QT designer'.

So I will take a good look at that either at lunch-time today or tonight and will try to update the example project I'm working on for you.

If I can get it building/working without the .ui files - my example will no longer require qtcreator/designer and should fit ALL of your requirements.

But then I'll have to re-draft the tutorial post I've started working on too!

Whilst I'm working on updating my 'Using a QT button to start a process' tutorial, you might also want to take a look at some of the QT documentation that I have linked in this post, including the complete addressbook tutorial (see 2nd link).

In order to be able to follow the tutorials and documentation - you will need to be familiar with some basic C++, including creating and using classes.

I'll also assume that you already have the appropriate qt5 libraries and tools installed.
 
@ryanvade
Yes, in a standard C/C++ application you could use fork and exec, or popen in a callback to fire off another process, but @blackneos940 was asking about QT, so I'm trying to keep it strictly QT related. QT has several classes that help with controlling processes and multithreading etc.

@blackneos940:
As an update - I finally found an up to date resource in the QT5 documentation which describes how to create a QT GUI by hand - without using qtcreator or qtdesigner:
https://doc.qt.io/qt-5/qtwidgets-tutorials-addressbook-part1-example.html

The above page is part of an addressbook tutorial I found in the "layouts" section of the qt5 Widgets documentation.


For now, I'm only interested in the UI design section where they create the UI using code, without having to use 'QT designer'.

So I will take a good look at that either at lunch-time today or tonight and will try to update the example project I'm working on for you.

If I can get it building/working without the .ui files - my example will no longer require qtcreator/designer and should fit ALL of your requirements.

But then I'll have to re-draft the tutorial post I've started working on too!

Whilst I'm working on updating my 'Using a QT button to start a process' tutorial, you might also want to take a look at some of the QT documentation that I have linked in this post, including the complete addressbook tutorial (see 2nd link).

In order to be able to follow the tutorials and documentation - you will need to be familiar with some basic C++, including creating and using classes.

I'll also assume that you already have the appropriate qt5 libraries and tools installed.
Ok..... :3 I'll sit here, studying the Java File you gave me, and I'm in no rush..... :) Take your time, good sir..... :3 I'll check out that Address Book thingy!..... :3
 
Look who's here!..... :3 I haven't talked to you in AGES!!..... :3 It's not strictly Qt-related, for all I know, but I'm trying to learn more C++, so I'll check it out!..... :D
It has been a while, yes.

I suppose if you want to stick solely with the Qt libs, the QProcess is what you are looking for.
http://doc.qt.io/qt-5/qprocess.html#details has a good example of running an external executable with QProcess
 
I suppose if you want to stick solely with the Qt libs, the QProcess is what you are looking for.
http://doc.qt.io/qt-5/qprocess.html#details has a good example of running an external executable with QProcess

Yup, QProcess is exactly what my example project uses to control the process. I know I mentioned QThread in my original reply here - but that was a good week or so before I had a chance to create the example project. But QProcess was the class I actually had in mind at the time. I just got the names muddled. I haven't used QT much in recent years, so it took a couple of days to get back up to speed with it!

@blackneos940:
Last night I made the necessary changes to my example project. So now it uses a hand-cranked GUI rather than relying on QTCreator/Designer's form designer and its UI:: framework. So it now builds entirely from the command-line.

So the example project is now complete and meets all of the requirements of your original post. Now I just need to finish drafting its accompanying tutorial. Hopefully, I'll have it ready to post in the next few days. It just depends how much time I get to write it!

It's going to take a little while though because I'm going to try and document the entire process of creating the app, rather than just supplying and explaining the final code for the application.
I think by documenting the processes used, it will aid you in creating your own projects.

So we'll start by building the shell of the application, with GUI and no functionality and then build it up from there as the tutorial progresses.

Also, as a heads up - I'll probably post the tutorial in a separate thread and then post a link to it here. But I'll also reference this thread in the tutorial too.

That way the tutorial will serve a dual purpose - as a standalone piece of content for the community and as a response to this thread.
 
Yup, QProcess is exactly what my example project uses to control the process. I know I mentioned QThread in my original reply here - but that was a good week or so before I had a chance to create the example project. But QProcess was the class I actually had in mind at the time. I just got the names muddled. I haven't used QT much in recent years, so it took a couple of days to get back up to speed with it!

@blackneos940:
Last night I made the necessary changes to my example project. So now it uses a hand-cranked GUI rather than relying on QTCreator/Designer's form designer and its UI:: framework. So it now builds entirely from the command-line.

So the example project is now complete and meets all of the requirements of your original post. Now I just need to finish drafting its accompanying tutorial. Hopefully, I'll have it ready to post in the next few days. It just depends how much time I get to write it!

It's going to take a little while though because I'm going to try and document the entire process of creating the app, rather than just supplying and explaining the final code for the application.
I think by documenting the processes used, it will aid you in creating your own projects.

So we'll start by building the shell of the application, with GUI and no functionality and then build it up from there as the tutorial progresses.

Also, as a heads up - I'll probably post the tutorial in a separate thread and then post a link to it here. But I'll also reference this thread in the tutorial too.

That way the tutorial will serve a dual purpose - as a standalone piece of content for the community and as a response to this thread.
Ok!..... :3 Again, take your time..... :3 Also, I'll keep an eye out for the link!..... :3 Thank you SO much, good sir..... :3 This will really help me in mastering Qt..... :3
 
OK, @blackneos940 - the tutorial has been posted here.

I haven't spent much time working on the tutorial, so it's a bit rough around the edges. But I'll try to make some improvements to it when I can!

But the code for the application is all there, and it's all done in a step by step manner, so you should be able to follow what is going on from start to finish.
 

Members online


Top