As described on https://wiki.qt.io/How_to_create_a_library_with_Qt_and_use_it_in_an_application I would like to create dll (Windows 10) and load it dynamically be using QLibrary as plugin.
In Dependency Walker I see that exports and dll could be loaded. But resolve gives me always 0. I tried several examples and hinds in web, but no success. What do I wrong?
.pro
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++11
TEMPLATE = lib
HEADERS += eurolite_dmx512_pro_mk2.h \
Eurolite_DMX512_Pro_MK2_global.h
SOURCES += eurolite_dmx512_pro_mk2.cpp
DEFINES += EUROLITE_DMX512_PRO_MK2_LIBRARY
global.h
#ifndef EUROLITE_DMX512_PRO_MK2_GLOBAL_H
#define EUROLITE_DMX512_PRO_MK2_GLOBAL_H
#include <QtCore/qglobal.h>
#if defined(EUROLITE_DMX512_PRO_MK2_LIBRARY)
# define EUROLITE_DMX512_PRO_MK2 Q_DECL_EXPORT
#else
# define EUROLITE_DMX512_PRO_MK2 Q_DECL_IMPORT
#endif
#endif // EUROLITE_DMX512_PRO_MK2_GLOBAL_H
.h
#ifndef SHARED_H
#define SHARED_H
#include "Eurolite_DMX512_Pro_MK2_global.h"
#include <QDebug>
class Device
{
public:
Device();
void Testdll() {qDebug("OK");}
};
extern "C" EUROLITE_DMX512_PRO_MK2 int getVersion();
extern "C" EUROLITE_DMX512_PRO_MK2 QWidget *createWidget1();
int EUROLITE_DMX512_PRO_MK2 libStart(void)
{
Device device;
return 0;
}
#endif // SHARED_H
.cpp
#include <QWidget>
#include "eurolite_dmx512_pro_mk2.h"
Device::Device() {}
int getVersion()
{
return 3;
}
QWidget *createWidget1()
{
QWidget *widget = new QWidget();
widget->resize(100, 100);
return widget;
}
plugintest.c++
#include "dmxservice.h"
#include <QApplication>
#include <QDebug>
#include <QLibrary>
#include <QMessageBox>
#include <QFile>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
bool dmxLibAktiv = false;
QLibrary dmxlib;
QMessageBox msgBox;
QFile file(QCoreApplication::applicationDirPath()+"/devices/Eurolite_DMX512_Pro_MK2.dll");
if(file.exists()==true)
{
QLibrary dmxlib(QCoreApplication::applicationDirPath()+"/devices/Eurolite_DMX512_Pro_MK2.dll");
if(dmxlib.load()==true)
{
dmxLibAktiv = dmxlib.isLoaded();
msgBox.setText("DLL loaded!\n");
msgBox.exec();
}
else
{
msgBox.setText("DLL not loaded!\n"+dmxlib.errorString());
msgBox.exec();
}
}
else
{
msgBox.setText("DLL not found '"+QCoreApplication::applicationDirPath()+"'!\n");
msgBox.exec();
}
if (dmxLibAktiv) {
typedef QWidget *(*CreateWidgetFunction)();
CreateWidgetFunction cwf = CreateWidgetFunction(dmxlib.resolve("createWidget1"));
if (cwf) {
QWidget *widget = cwf();
if (widget)
widget->show();
} else {
qDebug() << "Could not show widget from the loaded library";
}
}
DmxService w;
w.show();
return a.exec();
}
You created a local QLibrary dmxlib(...); inside the if (file.exists()) branch. So the outer dmxlib instance is never initialized.
ADDED: Do not re-declare the variable inside the if block, use setFileName() instead:
QLibrary dmxlib;
...
if (file.exists()) {
dmxlib.setFileName(QCoreApplication::applicationDirPath()+"/devices/Eurolite_DMX512_Pro_MK2.dll");
if (dmxlib.load())
....
}
// ... use dmxlib
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 have been facing issues with a simple C++ group project I am working on. I am trying to create a thread that does a background job while my UI and the main app is working perfectly fine. I tried to the same thing on a test file before and it worked fine, however doing it in the Qt Project doesn't works somehow. I do not have much idea about Qt for ref. Here's my code. I am trying to update the progress bar repeatedly till my app runs.
Here's my code for more over view
test code
#include <thread>
#include <iostream>
#include <windows.h>
int getRamUsage() {
MEMORYSTATUSEX statex;
statex.dwLength = sizeof (statex);
GlobalMemoryStatusEx (&statex);
return statex.dwMemoryLoad;
}
void ramUsage() {
while (true) {
std::cout << "RAM usage: " << getRamUsage() << "%" << std::endl;
Sleep(1000);
}
}
int main() {
auto ramLoop = [=]() {
while (true) {
std::cout << "RAM usage: " << getRamUsage() << "%" << std::endl;
Sleep(1000);
}
};
std::thread t1(ramLoop);
t1.join();
}
It works, But it doesn't in the Front Ended Qt one I mentioned. May I know any other ideas to make this work.
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "ramUsage.h"
#include <thread>
#include <windows.h>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
auto ramLoop = [=]() {
while (true) {
ui->ramBar->setValue(getRamUsage());
Sleep(1000);
}
};
std::thread t(ramLoop);
t.join();
}
MainWindow::~MainWindow()
{
delete ui;
}
main.cpp
#include "mainwindow.h"
#include <stdio.h>
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
ramUsage.cpp
#include <windows.h>
#include "../ramUsage.h"
int getRamUsage() {
MEMORYSTATUSEX statex;
statex.dwLength = sizeof (statex);
GlobalMemoryStatusEx (&statex);
return statex.dwMemoryLoad;
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
ramUsage.h
#pragma once
#ifndef RAMUSAGE_H
#define RAMUSAGE_H
int getRamUsage();
#endif // RAMUSAGE_H
I tried using QProcess and the basic threading options for windows which doesn't seem to work
Welcome I have a problem with scanning Wi-Fi to get all available connecting in Wi-Fi. I have writed so far this code:
#include <QCoreApplication>
#include <QNetworkConfigurationManager>
#include <QNetworkConfiguration>
#include <QDebug>
#include <QNetworkSession>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QNetworkConfigurationManager ncm;
QNetworkConfiguration cfg;
QNetworkConfiguration::StateFlags flags;
int count = QNetworkConfiguration::Active;
qDebug() << "Amount available connect in Wi-Fi :" << count;
qDebug() << ncm.allConfigurations(flags = 0);
return a.exec();
}
I have a problem with shows allConfigurations. I have read documentation
Qt Network Configuration Manager
but I do not know how to do that.
Scanning Wi-Fi using QNetworkAccessManager.
I use QNetworkConfigurationManager class to get all WiFi s availables and show all of them into QTreeWidget.
QNetworkConfigurationManager ncm;
netcfgList = ncm.allConfigurations();
.pro file:
QT += core gui network
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = WiFi
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
.cpp file:
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
findTimer = new QTimer();
findTimer->setInterval(1000);
connect(findTimer,&QTimer::timeout,this,&MainWindow::findActiveWirelesses);
findTimer->start();
foundCount = 0;
ui->treeWidgetWiFis->setColumnWidth(0,50);
ui->treeWidgetWiFis->setColumnWidth(1,200);
findActiveWirelesses();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::findActiveWirelesses()
{
QNetworkConfigurationManager ncm;
netcfgList = ncm.allConfigurations();
WiFisList.clear();
for (auto &x : netcfgList)
{
if (x.bearerType() == QNetworkConfiguration::BearerWLAN)
{
if(x.name() == "")
WiFisList << "Unknown(Other Network)";
else
WiFisList << x.name();
qDebug() << x.type();
}
}
for(int i=0; i<WiFisList.size(); i++)
{
bool exist = false;
QTreeWidgetItem * item = new QTreeWidgetItem();
for(int j=0; j<ui->treeWidgetWiFis->topLevelItemCount(); j++)
{
QTreeWidgetItem *index = ui->treeWidgetWiFis->topLevelItem(j);
QString str = index->text(1);
if(str == WiFisList[i])
{
exist = true;
break;
}
}
if(!exist)
{
item->setTextAlignment(0,Qt::AlignVCenter);
item->setTextAlignment(1,Qt::AlignHCenter);
item->setText(0,QString::number(++foundCount));
item->setText(1,WiFisList[i]);
ui->treeWidgetWiFis->addTopLevelItem(item);
}
}
}
.h file:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QTimer>
#include <QList>
#include <QInputDialog>
#include <QStandardItem>
#include <QStandardItemModel>
#include <QNetworkConfiguration>
#include <QNetworkConfigurationManager>
#include <QNetworkSession>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
int foundCount;
QNetworkConfiguration netcfg;
QStringList WiFisList;
QList<QNetworkConfiguration> netcfgList;
public slots:
void findActiveWirelesses();
private:
Ui::MainWindow *ui;
QTimer *findTimer;
QStandardItemModel* listModel;
QNetworkSession *session;
};
#endif // MAINWINDOW_H
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();
}
How does one disable tooltips on a Qt4 QToolBar?
Using QAction::setToolTip("") does nothing and I can't find any settings related to disabling tooltips on either a QAction or QToolbar!
Example:
Toolbar.h
#ifndef TOOLBAR_H
#define TOOLBAR_H
#include <QtGui>
class Toolbar : public QToolBar
{
Q_OBJECT
public:
Toolbar()
{
QAction *action = this->addAction("Action");
action->setToolTip("");
}
bool event(QEvent *event)
{
if(event->type() == QEvent::ToolTip)
{
qDebug() << "QEvent::ToolTip";
}
return QToolBar::event(event);
}
};
#include "moc_Toolbar.cpp"
#endif // TOOLBAR_H
main.cpp
#include <QtGui>
#include "Toolbar.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QMainWindow window;
Toolbar *toolbar = new Toolbar;
window.addToolBar(toolbar);
window.setCentralWidget(new QWidget());
window.show();
return app.exec();
}
An event filter has to be used in this scenario.
Toolbar.h
#ifndef TOOLBAR_H
#define TOOLBAR_H
#include <QtGui>
class Toolbar : public QToolBar
{
Q_OBJECT
public:
Toolbar()
{
QAction *action = this->addAction("Action");
}
bool eventFilter(QObject *object, QEvent *event)
{
if(event->type() == QEvent::ToolTip)
{
return true;
}
return false;
}
};
#include "moc_Toolbar.cpp"
#endif // TOOLBAR_H
main.cpp
#include <QtGui>
#include "Toolbar.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QMainWindow window;
Toolbar *toolbar = new Toolbar;
qApp->installEventFilter(toolbar);
window.addToolBar(toolbar);
window.setCentralWidget(new QWidget());
window.show();
return app.exec();
}
I'm not quite sure how to localize this to just the Toolbar but I don't like tooltips anyway so this is a quick way to disable all of them.