How can I obtain this information:
Total Memory
Free Memory
Memory used by current running application ?
I think Qt should have memory options, that would be platform-independent, but
I can't find it. So what can I do when I want to make a platform-independent application that shows memory state?
Unfortunately, there is nothing built into Qt for this. You must do this per-platform.
Here are some samples to get you started. I had to implement this in one of my apps just last week. The code below is still very much in development; there may be errors or leaks, but it might at least point you in the correct direction. I was only interested in total physical RAM, but the other values are available in the same way. (Except perhaps memory in use by the current application ... not sure about that one.)
Windows (GlobalMemoryStatusEx)
MEMORYSTATUSEX memory_status;
ZeroMemory(&memory_status, sizeof(MEMORYSTATUSEX));
memory_status.dwLength = sizeof(MEMORYSTATUSEX);
if (GlobalMemoryStatusEx(&memory_status)) {
system_info.append(
QString("RAM: %1 MB")
.arg(memory_status.ullTotalPhys / (1024 * 1024)));
} else {
system_info.append("Unknown RAM");
}
Linux (/proc/meminfo)
QProcess p;
p.start("awk", QStringList() << "/MemTotal/ { print $2 }" << "/proc/meminfo");
p.waitForFinished();
QString memory = p.readAllStandardOutput();
system_info.append(QString("; RAM: %1 MB").arg(memory.toLong() / 1024));
p.close();
Mac (sysctl)
QProcess p;
p.start("sysctl", QStringList() << "kern.version" << "hw.physmem");
p.waitForFinished();
QString system_info = p.readAllStandardOutput();
p.close();
Much better on POSIX OSes (Linux, Solaris, perhaps latest MacOS...) :
getrusage(...) secially look at ru_maxrss.
getrlimit(...) but I did not find any usefull info into.
sysconf(...) : _SC_PAGESIZE, _SC_PHYS_PAGES, _SC_AVPHYS_PAGES
sysinfo(...) : totalram, freeram, sharedram, totalswap,...
So much treasures on POSIX computers not available on Windows.
This is currently not possible in Qt. You would need to ifdef the different OS memory calls.
Related
I have followed numerous examples littered around the web for running a python script from a Qt app, but I just cannot get it to work. I've tried this:
QProcess unknown error
Amongst many other examples.
I have even directly copied examples which other people have said works, but with no success at all. I simply cannot get a python script to run or even launch a cmd window. I can, however, launch notepad.
This, for example is called when I press a button:
void MainWindow::test()
{
qint64 pID;
QProcess *myProcess = new QProcess(this);
QStringList arguments;
arguments << "/k cd /d " << "c:\path to be opened";
myProcess->startDetached("cmd.exe", arguments, "", &pID);
}
To no avail.
Also, this:
QProcess p;
QStringList params;
params.clear();
params.append("C:\\Software\\Qt\\StarLib\\cadstar_workspace_paths.py");
params.append("C:\\Software\\Qt\\StarLib\\TP1268_RDU5.csw");
params.append("C:\\Software\\Qt\\StarLibcadstar_local_user_directories.txt");
p.setWorkingDirectory("C:\\Software\\Qt\\StarLib");
p.setProgram("py");
p.setArguments(params);
p.start();
if (!p.waitForStarted()) {
ui->textEdit->append("Could not start process");
return;
}
else
ui->textEdit->append("The program seems to have started.");
QTime time;
time.start();
while (time.elapsed() < 4000)
QApplication::processEvents();
p.kill();
p.waitForFinished();
With this code, it reports that the application seems to have started, even though it hasn't.
Can someone point me in the right direction please? Am happy to post any further relevant information that would help. I'm clearly missing something fundamental here!
I have a file.m, which is a file from matlab, and I want it to run when I run my qt project.
I noticed I could use this -r MATLAB_command - Start MATLAB and execute the MATLAB_command.
and this,
-nodesktop - Do not start the MATLAB desktop. Use the current
terminal for commands. The Java virtual machine
will be started.
from the matlab help, but i'm not sure if I'm thinking the right way.
My main.cpp
#include <QtGui/QApplication>
#include <iostream>
using namespace std;
#include <cmath>
#include <QProcess>
#include "planevolume.h"
#include "dialog.h"
int main(int argc, char *argv[])
{
// Start MATLAB MAIN.m
QProcess* p = new QProcess(NULL);
p->start( QString( "/usr/local/MATLAB/R2011b/bin/matlab"),
QStringList() << QString("-r /home/matt/Desktop/PlaneVolumeExec/MAIN.m")
<< QString("-nosplash")
<< QString("-nodesktop"));
QApplication app(argc, argv);
Dialog *dialog= new Dialog;
if (dialog->exec())
{
planevolume mainwindow(dialog->getdirprefix(),dialog->getxpax(), dialog->getypax(), dialog->getzpax(), dialog->getxmmax(), dialog->getymmax(), dialog->getzmmax(), dialog->getintzminp(), dialog->getintzmaxp(), dialog->getintzminm(), dialog->getintzmaxm());
mainwindow.show();
return app.exec();
}
return 0;
}
Matlab help
/*-h|-help - Display arguments.
-n - Display final environment variables,
arguments, and other diagnostic
information. MATLAB is not run.
-e - Display ALL the environment variables and
their values to standard output. MATLAB
is not run. If the exit status is not
0 on return then the variables and values
may not be correct.
-arch - Start MATLAB assuming architecture arch.
v=variant - Start the version of MATLAB found
in bin/glnxa64/variant instead of bin/glnxa64.
v=arch/variant - Start the version of MATLAB found
in bin/arch/variant instead of bin/glnxa64.
-c licensefile - Set location of the license file that MATLAB
should use. It can have the form port#host or
be a colon separated list of license files.
The LM_LICENSE_FILE and MLM_LICENSE_FILE
environment variables will be ignored.
-display Xdisplay - Send X commands to X server display, Xdisplay.
-nodisplay - Do not display any X commands. The MATLAB
desktop will not be started. However, unless
-nojvm is also provided the Java virtual machine
will be started.
-nosplash - Do not display the splash screen during startup.
-mwvisual visualid - The default X visual to use for figure windows.
-debug - Provide debugging information especially for X
based problems.
-desktop - Allow the MATLAB desktop to be started by a
process without a controlling terminal. This is
usually a required command line argument when
attempting to start MATLAB from a window manager
menu or desktop icon.
-nodesktop - Do not start the MATLAB desktop. Use the current
terminal for commands. The Java virtual machine
will be started.
-nojvm - Shut off all Java support by not starting the
Java virtual machine. In particular the MATLAB
desktop will not be started.
-jdb [port] - Enable remote Java debugging on port (default 4444)
-r MATLAB_command - Start MATLAB and execute the MATLAB_command.
-logfile log - Make a copy of any output to the command window
in file log. This includes all crash reports.
-Ddebugger [options] - Start debugger to debug MATLAB.*/
QProcess* p = new QProcess( this );
p->start( "%MATHLAB_EXE_FILE_FULL_PATH%", "%FILE_M_FULL_PATH%" );
so it will be like:
p->start( QString( "C:/Program Files/MatLab 9.0/matlab.exe" ),
QStringList() << QString( "-r D:/My files/matlab/file.m" )
<< QString( "-nosplash" ) );
Try this. It works.
QString program = "C:\\Program Files\\MATLAB\\R2017a\\bin\\matlab.exe";
myProcess->start(program, QStringList() << QString("-nodisplay")<< QString("-nodesktop")<< QString("-nosplash")<<QString("-r")<<QString("run('C:/Users/eleazar.balbon/Documents/MATLAB/Sample.slx');"));
myProcess->waitForFinished();
The answer below may not work beacuse QProcess added quotes to all parametrs in QStringList
You shuld try QProcess::setNativeArguments ( const QString & arguments )
If this also worn't work you can try QProcess::execute ( const QString & program ) [static]
QProcess::execute("My_programm.bin -arg1 val1 -arg2 val2");
I think this is the best way of running uncotrolled external processes.
If you create a QProcess instance and dont delete it manualy it well get you program running until of preces exits. Or if you app exits with help of kill or somethimg else you will have a qDebug issue of deleting of uncompleted proces(dont remeber exect string of this issue in qDebug). In different OS this may cause crush of your app(for example i have this problem on Windows XP)
It might be too late. But, for future references. This is what you should do.
In my script, I'm sending a path that is a variable in mine .m file called ae_run. This is how I have done it.
QProcess* p = new QProcess(NULL);
QString matlab_exe ="matlab /r -nosplash -nodesktop";
QString fileName_path=matlab_exe +" "+ "rec_path='"+path+"\';ae_run";
p->start(fileName_path);
qDebug()<<p->state()<<endl;
rec_path is the variable that I parse to my code, and the fileName is the path. At the end, when I run it. Looks like this.
"matlab /r -nosplash -nodesktop rec_path='D:\\robot_IMU_data\\file_data.txt';ae_run"
If you see, it is like running it from the command line
I've been browsing the Qt source code trying to find the actual system calls but it seems Qt doesn't use the Windows API documented on MSDN. For example grepping the source for "GetClipboardData" returns results in two files:
qclipboard_win.cpp:
#if defined(Q_OS_WINCE)
...
HANDLE clipData = GetClipboardData(CF_TEXT)
qaxserverbase.cpp:
STDMETHOD(GetClipboardData)(DWORD dwReserved, IDataObject** ppDataObject);
...
HRESULT WINAPI QAxServerBase::GetClipboardData(DWORD, IDataObject**)
{
return E_NOTIMPL;
}
and "SetClipboardData":
qclipboard_win.cpp:
#if defined(Q_OS_WINCE)
...
result = SetClipboardData(CF_UNICODETEXT, wcsdup(reinterpret_cast<const wchar_t *> (data->text().utf16()))) != NULL;
Neither of which seems useful, since they're being declared for Win CE/Mobile.
My Qt (4.8.1) uses OleSetClipboard and OleGetClipboard. The lines you got to are never reached in regular windows, as only in case of #if defined(Q_OS_WINCE) Qt uses #define OleSetClipboard QtCeSetClipboard and #define OleGetClipboard QtCeGetClipboard, and otherwise uses system-provided versions of those functions.
It was a little dificult to see this #if defined though, so you are excused ;)
It is so at least on my Qt version. If you are talking about qt, and especially about it's internals, you should menstion the version, right?
Does Qt maintain any sort of versioning information about your program like .NET does? Like the build number? Or does it provide an easy way to access the SVN revision?
No.
But if you're using qmake then you can set compiler flags in the build system based on the results of arbitrary commands, which might be usable to do what you want.
For example, if you were using git, you could do something like this in your .pro file:
REVISION = $$system(git rev-parse HEAD)
DEFINES += APP_REVISION=$$REVISION
That would give you an APP_REVISION macro when compiling your program, which you could use like this:
// stringize macro
#define _STR(X) #X
#define STR(X) _STR(X)
QTextStream(cout) << "MyApp revision " STR(APP_REVISION) << endl;
Is there a (Qt) way to determine the platform a Qt application is running on at runtime?
Intention: While I hate to bring up a question
that is almost 2 years old, I think
that a good amended answer is valuable
to have on record so that others that
end up on this question can do it the
right way.
I can't help but notice that most of the answers recommend using the Q_WS set of macros to determine the Operating System, this is not a good solution, since Q_WS_* refers to the Windowing System and not the Operating System platform(for eg. X11 can be run on Windows or Mac OS X then what?), thus one should not follow those macros to determine the platform for which the application has been compiled.
Instead one should use the Q_OS_* set of macros which have the precise purpose of determining the Operating System.
The set currently consists of the following macros for Qt 4.8:
Q_OS_AIX
Q_OS_BSD4
Q_OS_BSDI
Q_OS_CYGWIN
Q_OS_DARWIN
Q_OS_DGUX
Q_OS_DYNIX
Q_OS_FREEBSD
Q_OS_HPUX
Q_OS_HURD
Q_OS_IRIX
Q_OS_LINUX
Q_OS_LYNX
Q_OS_MAC
Q_OS_MSDOS
Q_OS_NETBSD
Q_OS_OS2
Q_OS_OPENBSD
Q_OS_OS2EMX
Q_OS_OSF
Q_OS_QNX
Q_OS_RELIANT
Q_OS_SCO
Q_OS_SOLARIS
Q_OS_SYMBIAN
Q_OS_ULTRIX
Q_OS_UNIX
Q_OS_UNIXWARE
Q_OS_WIN32
Q_OS_WINCE
References:
Qt 4 https://doc.qt.io/archives/qt-4.8/qtglobal.html
Qt 5 https://doc.qt.io/qt-5/qtglobal.html
Qt 6 https://doc.qt.io/qt-6/qtglobal.html
NB: As mentioned by Wiz in the comments, Qt 5 completely removed the Q_WS_* set of macros, thus now all you can use are Q_OS_* ones.
Note that the Q_WS_* macros are defined at compile time, but QSysInfo gives some run time details.
To extend gs's function to get the specific windows version at runtime, you can do
#ifdef Q_WS_WIN
switch(QSysInfo::windowsVersion())
{
case QSysInfo::WV_2000: return "Windows 2000";
case QSysInfo::WV_XP: return "Windows XP";
case QSysInfo::WV_VISTA: return "Windows Vista";
default: return "Windows";
}
#endif
and similar for Mac.
If you are using a Qt version 5.9 or above, kindly use the below mentioned library function to retrieve correct OS details, more on this can be found here. There is also a QSysInfo class which can do some additional functionalities.
#ifdef Q_WS_WIN
#include <QOperatingSystemVersion>
switch(QOperatingSystemVersion::current())
{
case QOperatingSystemVersion::Windows7: return "Windows 7";
case QOperatingSystemVersion::Windows8: return "Windows 8";
case QOperatingSystemVersion::Windows10: return "Windows 10";
default: return "Windows";
}
#endif
For Qt5 I use the following:
logging.info("##### System Information #####")
sysinfo = QtCore.QSysInfo()
logging.info("buildCpuArchitecture: " + sysinfo.buildCpuArchitecture())
logging.info("currentCpuArchitecture: " + sysinfo.currentCpuArchitecture())
logging.info("kernel type and version: " + sysinfo.kernelType() + " " + sysinfo.kernelVersion())
logging.info("product name and version: " + sysinfo.prettyProductName())
logging.info("#####")
Documentation: http://doc.qt.io/qt-5/qsysinfo.html
Here is part of my code to detect windows or mac at run time and the version
#include <QSysInfo>
#include <QOperatingSystemVersion>
auto OSType= OSInfo.type();
auto OSInfo = QOperatingSystemVersion::current();
if (OSType !=1) //not windows os
{
return 0;
}
if (OSInfo < QOperatingSystemVersion::Windows7) // less than win7
{
return 0;
}