Can you configure Qt Creator to kill the running a

2019-03-27 02:08发布

I'm developing a QApplication and I find that I often forget to close my application before rebuilding and re-running it. This becomes a problem when I accidentally look at an instance of my application built 10 minutes ago and expect to see code changes from 30 seconds ago.

It is also annoying because I have to move my hand all the way over to the mouse, move the mouse all the way over to the running application, and close it every time I want to rebuild and I am lazy and I don't want to do that.

Is it possible to configure Qt Creator to close my QApplication when I start building or running it again?

标签: qt-creator
5条回答
Rolldiameter
2楼-- · 2019-03-27 02:41

Try to add the following Custom Process Step at the top of the Build Steps

enter image description here

查看更多
放我归山
3楼-- · 2019-03-27 02:42

I am on Windows so the killall does not work. But I figured this out (enter the values in the same form that Julien has posted as a screenshot):

Command: taskkill

Arguments: /FI "STATUS eq Running" /IM APPNAME.exe /F

You need to replace APPNAME with your application's name, of course. The /FI "STATUS eq Running" is a filter that enables qmake to proceed after this pre-build-step, even if the application is not yet running (else it would not find the process to kill and then exit). Without the /F the application won't get closed, at least for me.

However, if your application has a icon in the trayarea, the icon won't disappear. The icon gets removed when you move the mouse over it.

查看更多
我只想做你的唯一
4楼-- · 2019-03-27 02:57

There is an option to do that in Qt Creator:

  1. Open the Preferences => Build & Run => General
  2. Change “Stop application before building” from “None” to “Same Project”
查看更多
爷的心禁止访问
5楼-- · 2019-03-27 02:57

To whomever might stumble here later, an easier way to o this is to put a "killall" command (as suggested by tangbongbong) is the first build step under Projects(ctrl+5) -> Build and Run

In the screenshot below my executable name was "Tutano" and I added the -9 option to ensure it would be killed.

Screenshot - sorry, I'm new here so can't post an image directly

查看更多
\"骚年 ilove
6楼-- · 2019-03-27 03:05

Yes.

One way is to create a very simple script that kills your existing running process and starts a new one. If your program executable is FooBarProgram then go to the build directory that Qt has created and create this file FooBarProgramLauncher

#!/bin/bash

killall FooBarProgram
./FooBarProgram

Now change the Qt project configuration to run your launcher instead running your program directly:

  1. Open the project in Qt Creator
  2. Select "Projects" from the pane on the left
  3. Select the "Build & Run" tab if not already selected
  4. Select the "Run" subtab
  5. Under the "Run" heading, click the "Add" button, select "Custom Executable"
  6. On the "Command" line, click the "Browse" button and select FooBarProgramLauncher

Now whenever you tell Qt to run your program it will run FooBarProgramLauncher instead which in turn will kill any running instances of FooBarProgram and then run the newly compiled executable.

查看更多
登录 后发表回答