Hi I am learning C++ through QT and I'm on the part where I'm trying to get LASTINPUTINFO to work. Below is the code I have made to see how it works but it seems to be only returning a single value and doesn't ever change whenever I make any inputs.
Care to explain what I'm doing wrong? And maybe provide a working example so I could get a grasp.
I'm trying to run it on Windows 10 Pro 64-bit.
#include <QtCore/QCoreApplication>
#include <QDebug>
#include <Windows.h>
#include <unistd.h>
using namespace std;
test()
{
LASTINPUTINFO lastii;
lastii.cbSize = sizeof(LASTINPUTINFO);
return lastii.dwTime;
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
while (true) {
qDebug() << test();
sleep(1);
}
return a.exec();
}
Example output here.
138899896
138899896
138899896
138899896
138899896
138899896
138899896
Fixed code for reference. Thanks to Anders.
#include <QtCore/QCoreApplication>
#include <QDebug>
#include <Windows.h>
#include <unistd.h>
#include <iostream>
using namespace std;
test()
{
LASTINPUTINFO lastii;
lastii.cbSize = sizeof(LASTINPUTINFO);
GetLastInputInfo(&lastii);
return (GetTickCount() - lastii.dwTime) / 1000;
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
while (true) {
cout<<test()<<"\n";
sleep(1);
}
return a.exec();
}
LASTINPUTINFO is not a class, it is a simple C struct. You actually have to call a function to fill it:
DWORD test() {
LASTINPUTINFO lastii;
lastii.cbSize = sizeof(LASTINPUTINFO);
GetLastInputInfo(&lastii);
return lastii.dwTime;
}
Related
I am new in QT 4 C++ .I have QT 4 form with QTabwidget like in th picture below.
enter image description here
I want to disply on console the string "aa" by selecting tab on clicking it.
Here is my newForm.h
#ifndef _NEWFORM_H
#define _NEWFORM_H
#include "qwidget.h"
#include "ui_newForm.h"
class newForm : public QDialog {
Q_OBJECT
public:
newForm();
virtual ~newForm();
private:
Ui::newForm widget;
};
#endif /* _NEWFORM_H */
_________________________________________________________________________________________
Here is my main.cpp
#include <QApplication>
#include "newForm.h"
int main(int argc, char *argv[]) {
// initialize resources, if needed
// Q_INIT_RESOURCE(resfile);
QApplication app(argc, argv);
newForm *a = new newForm();
a->show();
// create and show your widgets here
return app.exec();
}
Here is my newForm.cpp
#include "newForm.h"
#include <iostream>
#include <QDebug>
newForm::newForm() {
connect(widget.tabWidget, SIGNAL(stateChanged()), this, SLOT(onTabChanged(int)));
widget.setupUi(this);
}
newForm::~newForm() {
}
void newForm::onTabChanged(int ){
qDebug()<<"aa"<<"\n";
}
On selecting new tab it is not displaying "aa"?How to qDebug() "aa" on console?
First of all, why are you using Qt 4 in 2022?
Next thing use currentIndexChanged(int) istead of stateChanged().
Did you also noticed that stateChanged() doesnt pass an interger which is needed for onTabChanged(int).
You also connected widget.tabWidget which isn't initilized yet.
This code might work:
newForm.h
#ifndef _NEWFORM_H
#define _NEWFORM_H
#include "qwidget.h"
namespace Ui { class newForm; };
class newForm : public QDialog {
Q_OBJECT
public:
newForm();
~newForm();
private:
Ui::newForm *widget;
};
#endif /* _NEWFORM_H */
newForm.cpp
#include "newForm.h"
#include "ui_newForm.h"
#include <QDebug>
newForm::newForm()
: widget(new Ui::newForm)
{
widget->setupUi(this);
connect(widget->tabWidget, SIGNAL(currentChanged(int)), this, SLOT(onTabChanged(int)));
}
void newForm::onTabChanged(int ){
qDebug() << "aa";
}
newForm::~newForm() {
delete widget;
}
main.cpp
#include <QApplication>
#include "newForm.h"
int main(int argc, char *argv[]) {
// initialize resources, if needed
// Q_INIT_RESOURCE(resfile);
QApplication app(argc, argv);
newForm a;
a.show();
// create and show your widgets here
return app.exec();
}
I'm following this document and I'm using qt to implement this event
#include <QCoreApplication>
#include <QDebug>
#include <libvirt/libvirt.h>
void
domainLifecycleCb(virConnectPtr conn,
virDomainPtr dom,
void * opaque)
{
qDebug() << "test";
}
int main(int argc, char *argv[])
{
virEventRegisterDefaultImpl();
QCoreApplication a(argc, argv);
virConnectPtr conn = virConnectOpen("qemu:///session");
virDomainPtr domain = virDomainLookupByName(conn, "Windows");
qDebug() << virConnectDomainEventRegisterAny(conn,
domain,
VIR_DOMAIN_EVENT_ID_LIFECYCLE,
VIR_DOMAIN_EVENT_CALLBACK(domainLifecycleCb),
NULL, NULL);
return a.exec();
}
I tried different methods but the event doesn't work when the domain gets shutdown or started.
By that I mean the callback function should get call when the domain life-cycle changes, And I'm determining that by showing an output in the callback function.
There is a function that needs to be called in a loop to keep the connection alive
#include <QCoreApplication>
#include <QDebug>
#include <libvirt/libvirt.h>
#include <QThread>
void domainLifecycleCb(virConnectPtr conn,
virDomainPtr dom,
void * opaque)
{
qDebug() << "test";
}
int main(int argc, char *argv[])
{
virEventRegisterDefaultImpl();
QCoreApplication a(argc, argv);
virConnectPtr conn = virConnectOpen("qemu:///session");
virDomainPtr domain = virDomainLookupByName(conn, "WindowsECO");
QThread *thread = QThread::create([]{
while (true) {
qDebug() << virEventRunDefaultImpl();
} });
thread->start();
qDebug() << virConnectDomainEventRegisterAny(conn,
domain,
VIR_DOMAIN_EVENT_ID_LIFECYCLE,
VIR_DOMAIN_EVENT_CALLBACK(domainLifecycleCb),
NULL, NULL);
return a.exec();
}
I have a very simple application using WebEngineView and I just wanted to resize the widget displaying to the contents of the html file. I'm expecting it to be 30 pixels wide. Instead my program prints QSize(0,0) and even worser the widget is not displayed at all.
What I'm doing wrong here?
#include <QWebEngineView>
#include <QApplication>
#include <QDebug>
#include <QWebEnginePage>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
auto view = new QWebEngineView;
QString html = "<html><body><div width=30px>Text</div></body></html>";
view->setHtml(html);
auto contentsSize=view->page()->contentsSize().toSize();
qDebug() << contentsSize;
view->setFixedSize(contentsSize);
view->show();
return app.exec();
}
Putting my QWebEngineView into a dialog still doesn't work:
#include <QWebEngineView>
#include <QApplication>
#include <QDebug>
#include <QDialog>
#include <QHBoxLayout>
#include <QWebEnginePage>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
auto dialog = new QDialog;
dialog->setLayout(new QHBoxLayout);
auto view = new QWebEngineView;
dialog->layout()->addWidget(view);
QString html = "<html><body><div width=30px>Text</div></body></html>";
view->setHtml(html);
auto contentsSize=view->page()->contentsSize().toSize();
qDebug() << contentsSize;
view->setFixedSize(contentsSize);
dialog->show();
return app.exec();
}
I also tried to connect to the signal loadFinished, but there is no effect.
#include <QWebEngineView>
#include <QApplication>
#include <QDebug>
#include <QDialog>
#include <QHBoxLayout>
#include <QWebEnginePage>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
auto dialog = new QDialog;
dialog->setLayout(new QHBoxLayout);
auto view = new QWebEngineView;
dialog->layout()->addWidget(view);
QString html = "<html><body><div width=30px>Text</div></body></html>";
view->setHtml(html);
QObject::connect(view->page(), &QWebEnginePage::loadFinished, [&view](bool b) {
qDebug() << b;
auto contentsSize = view->page()->contentsSize().toSize();
qDebug() << contentsSize;
view->setFixedSize(contentsSize);
});
dialog->show();
return app.exec();
}
I'm trying a very, very simple QT networking program. For some reason it crashes when executing without any error message, since it's not printing out any of the outputs to the command line as expected. Here's the code:
qtTCPservertest.pro
QT += core
QT += network
QT -= gui
TARGET = qtTCPservertest
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp \
theserver.cpp
HEADERS += \
theserver.h
theServer.h
#ifndef THESERVER_H
#define THESERVER_H
#include <QTcpServer>
#include <stdio.h>
class theServer : public QTcpServer{
Q_OBJECT
public:
theServer();
~theServer();
void goOnline();
};
#endif // THESERVER_H
theServer.cpp
#include "theserver.h"
theServer::theServer()
{
}
theServer::~theServer()
{
}
void theServer::goOnline()
{
bool status = false;
unsigned int portNum = 5200;
status = this->listen(QHostAddress::Any, portNum );
// Check, if the server did start correctly or not
if( status == true )
printf("Server up\n");
else
printf("Server down\n");
}
and the main.cpp
#include <QCoreApplication>
#include <stdio.h>
#include "theserver.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
printf("Test\n");
theServer* aServer = new theServer();
aServer->goOnline();
aServer->~theServer();
return a.exec();
}
Has anyone an idea, where I went wrong? Since there is no error I'm total clueless. It just doesn't print out anything, it just tells me to hit any key to close the window, as if it came to an end as usual.
Thanks for any advise.
Here is the code that compiles and works for me (Qt 5.5):
TheServer.h
#ifndef THESERVER_H
#define THESERVER_H
#include <QTcpServer>
class TheServer : public QTcpServer
{
Q_OBJECT
public:
TheServer(QObject *pParent = nullptr);
void goOnline();
};
#endif // THESERVER_H
TheServer.cpp
#include <QDebug>
#include "TheServer.h"
TheServer::TheServer(QObject *pParent)
: QTcpServer(pParent)
{
}
void TheServer::goOnline()
{
bool status = listen(QHostAddress::Any, 5200);
if (status) {
qDebug() << "Server up";
} else {
qDebug() << "Server down";
}
}
main.cpp
#include <QCoreApplication>
#include "TheServer.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
TheServer server;
server.goOnline();
return a.exec();
}
I'm trying to create QApplication in a different thread, but found 2 main problems:
1- I can't interact with GUI
2- some warnings:
WARNING: QApplication was not created in the main() thread.
QObject::startTimer: timers cannot be started from another thread //happens when resizing widget
QObject::killTimer: timers cannot be stopped from another thread
here is the full code: (it may has some memory leaks but for testing purposes it fails)
//main.cpp
#include <QCoreApplication>
#include "cthread.h"
int main(int argc, char *argv[])
{
CThread *MyThread = new CThread;
MyThread->start();
QCoreApplication a(argc, argv);
return a.exec();
}
//CThread.h
#ifndef CTHREAD_H
#define CTHREAD_H
#include <QThread>
#include "theqtworld.h"
class CThread : public QThread
{
Q_OBJECT
public:
CThread();
void run( void );
private:
TheQtWorld *mWorld;
};
#endif // CTHREAD_H
//CThread.cpp
#include "cthread.h"
#include <iostream>
CThread::CThread():mWorld(NULL)
{
}
void CThread::run()
{
std::cout << "thread started" << std::endl;
if(!mWorld)
mWorld = new TheQtWorld();
mWorld->OpenWorld();//now it will init all Qt Stuff inside
// if(mWorld) delete mWorld;
// emit this->exit();
}
//theqtworld.h
#ifndef THEQTWORLD_H
#define THEQTWORLD_H
#include <QObject>
#include "mainwindow.h"
#include <QApplication>
class TheQtWorld : public QObject
{
Q_OBJECT
public:
explicit TheQtWorld(QObject *parent = 0);
int OpenWorld(void);
signals:
public slots:
};
#endif // THEQTWORLD_H
//theqtworld.cpp
#include "theqtworld.h"
TheQtWorld::TheQtWorld(QObject *parent) :
QObject(parent)
{
}
int TheQtWorld::OpenWorld()
{
static int arg = 0;
static char *b[2];
b[0] = "a";
QApplication *a = new QApplication(arg, b);
a->setParent(this);
MainWindow w;
w.show();
return a->exec();
}
I would answer my own question after understanding how to overcome this problem
first the problem was to integrate Qt GUI as a plugin into another Application, so the main issue was the Event loop collision between Qt Events and any other Application Events
my first thoughts was to separate both, so QApplication will stay at a different thread, but this was a totally wrong approach and here is what I have noticed:
1- Qt GUI Must stay in the main() thread so there is no other place for QApplication
2- to avoid the blocking QApplication::exec() , embed QApplication::processEvents() into the other Application Event loop
here is a working code:
//main.cpp
#include <QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication mApp(argc, argv);
MainWindow w;
w.show();
//just for testing and holding the program so it doesn't end
for(int i = 0; i < 100000000; ++i)
{
mApp.processEvents();
}
return 0;
}
edit:thanks to pavel-strakhov for his great suggestion.