I want use QTest Macro QCOMPARE in my code,but I receive errors.
QTestString.h
#ifndef QTESTSTRING_H
#define QTESTSTRING_H
#include <QtCore/QString>
#include <QtTest/QtTest>
class TestqstringTest : public QObject
{
Q_OBJECT
public:
TestqstringTest();
private slots:
void testCase1();
};
#endif // QTESTSTRING_H
QTestString.cpp
#include "QTestString.h"
TestqstringTest::TestqstringTest()
{
testCase1();
}
void TestqstringTest::testCase1()
{
QString str = "Hello";
QCOMPARE(str.toUpper(),(QString)"hELLO");
}
main.cpp
#include "QTestString.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
TestqstringTest *test = new TestqstringTest();
return a.exec();
}
However, I receive the following errors:
ASSERT: "QTest::testLogger" in file qtestlog.cpp, line 266 The program
has unexpectedly finished.
I found the answer,you must use int QTest::qExec ( QObject * testObject, int argc = 0, char ** argv = 0 ) to excute it ,then the testlog output correctly.
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 am completely new to QT and I want to prepare one window and take some input from the user then with this input run one console and show output in the console. I have tried to write code after exec but it seems it is not possible:
int main(int argc, char *argv[])
{
int retmain = 0;
QApplication a(argc, argv);
MainWindow w;
w.show();
cout<<"pos500"<<endl;
retmain = a.exec();
cout<<"pos50"<<endl;
//doing something
return retmain;
}
I don't know why but after a.exec(); nothing happens.
So I searched on the internet and found below topic in stackoverflow:
How to call function after window is shown?
But I want to end the graphic window and then do my process.
You need to call QCoreApplication::exit() to make exec return control to you.
After this function has been called, the application leaves the main event loop and returns from the call to exec(). The exec() function returns returnCode. If the event loop is not running, this function does nothing.
A simple example would be:
//mainwindow.h
//////////////////////////////////////////////////
#pragma once
#include <QtWidgets/QMainWindow>
#include <QtCore/QCoreApplication>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
void closeEvent(QCloseEvent *event);
~MainWindow();
};
//mainwindow.cpp
//////////////////////////////////////////////////
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
}
void MainWindow::closeEvent(QCloseEvent *event)
{
QCoreApplication::exit(0);
QMainWindow::closeEvent(event);
}
MainWindow::~MainWindow(){}
//main.cpp
//////////////////////////////////////////////////
#include "mainwindow.h"
#include <QApplication>
#include <iostream>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
a.exec();
std::cout << "test" << std::endl;
return 0;
}
I have the need (for example when building a library) to instantiate QCoreApplication on the heap, and I found the following strange behavior (Qt 5.7):
#include <QCoreApplication>
#include <QDebug>
class Test
{
public:
Test(int argc, char *argv[]) {
m_app = new QCoreApplication(argc, argv);
//uncomment this line to make it work
//qDebug() << "test";
}
~Test() { delete m_app; }
private:
QCoreApplication* m_app;
};
int main(int argc, char *argv[])
{
Test test(argc, argv);
qDebug() << QCoreApplication::arguments(); //empty list!
}
Basically, everything works as expected if "qDebug()" is used just after allocating the object. If not, the list of arguments() is empty.
It seems to be related to this bug, which was fixed in Qt 5.9 and backported to Qt 5.6.3. The workaround is simply:
#include <QCoreApplication>
#include <QDebug>
class Test
{
public:
Test(int argc, char *argv[]) {
//allocate argc on the heap, too
m_argc = new int(argc);
m_app = new QCoreApplication(*m_argc, argv);
}
~Test() {
delete m_app;
delete m_argc;
}
private:
int* m_argc;
QCoreApplication* m_app;
};
int main(int argc, char *argv[])
{
Test test(argc, argv);
qDebug() << QCoreApplication::arguments();
}
I believe another way to fix this bug is to pass argc by reference:
#include <QCoreApplication>
#include <QDebug>
class Test
{
public:
Test(int& argc, char *argv[]) {
m_app = new QCoreApplication(argc, argv);
//uncomment this line to make it work
//qDebug() << "test";
}
~Test() { delete m_app; }
private:
QCoreApplication* m_app;
};
int main(int argc, char *argv[])
{
Test test(argc, argv);
qDebug() << QCoreApplication::arguments(); //empty list!
}
In addition, you don't need to create QCoreApplication on the heap, having it as an automatic member of Test is fine, i.e. QCoreApplication m_app.
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.