If I run this constructor
USB::USB(){
serialPort = new QSerialPort();
serialPortInfo = new QSerialPortInfo();
}
From this class
#include <QSerialPort>
#include <QSerialPortInfo>
class USB {
public:
USB();
private:
QSerialPort* serialPort;
QSerialPortInfo* serialPortInfo;
};
Then I got the error
The program has unexpectedly finished.
If I comment the code
USB::USB(){
//serialPort = new QSerialPort();
//serialPortInfo = new QSerialPortInfo();
}
Then I get no errors at all.
Why does this happens? I have missed something?
I'm running QT5 at Windows 10.
This gives no errors. Getting information about the COM ports works.
USB::USB(QSerialPort* serialPort, const QSerialPortInfo& serialPortInfo){
this->serialPort = serialPort;
this->serialPortInfo = serialPortInfo;
}
Related
I use the old connection syntax until now. Today I want to change my project to new connection syntax like this:
#include <QDebug>
class Telefon : public QObject {
Q_OBJECT
public:
void letRing() {
qDebug() << "letRing";
emit ring();
}
signals:
void ring();
public slots:
void onRing() {
qDebug() << "onRing";
}
};
class Cable : public QObject {
Q_OBJECT
signals:
void ring();
};
int main(int argc, char *argv[])
{
Telefon *fon1 = new Telefon();
Telefon *fon2 = new Telefon();
Cable *cable = new Cable();
// old connection way
// connect(fon1, SIGNAL(ring()), cable, SIGNAL(ring()));
// connect(cable, SIGNAL(ring()), fon2, SLOT(onRing()));
// new connection way
QObject::connect(fon1, &Telefon::ring, cable, &Cable::ring); // <-- This is the problem
QObject::connect(cable, &Cable::ring, fon2, &Telefon::onRing);
fon1->letRing();
return 0;
}
The compiling went well with no errors. But the telefon never rings :(
Output should be:
letRing
onRing
But it is:
letRing
I Dig into the Qt code and found that SIGNAL-SLOT connections where valid but SIGNAL-SIGNAL connections don't work this way. Beside it generate no error while compiling. Is there a way to connect SIGNALs the new way/syntax?
I have tried the fellowing two methods but failed:
QAndroidJniObject activity = QAndroidJniObject::callStaticObjectMethod("org/qtproject/qt5/android/QtNative", "activity", "()Landroid/app/Activity;");
QPlatformNativeInterface *interface = QApplication::platformNativeInterface();
jobject activity = (jobject)interface->nativeResourceForIntegration("QtActivity");
Ok, I was in QT 5.2 and this worked for me:
first:
in your .pro:
QT += gui-private
now in your cpp:
#include <QAndroidJniObject>
#include <qpa/qplatformnativeinterface.h>
QAndroidJniObject getMainActivity()
{
QPlatformNativeInterface *interface = QApplication::platformNativeInterface();
QAndroidJniObject activity = (jobject)interface->nativeResourceForIntegration("QtActivity");
if(!activity.isValid())
qDebug()<<"CLASS NOT VALID!!!!!!!!";
else
qDebug()<<"HORRAY!";
return activity;
I'm doing a project using Qt with some customized QDialogs for user input. Due to hardware constraint of my development box, I want to monitor the memory usage of my app. How I exec the dialog.
1 void MainWindow::callDialog() {
2 DlgPopConfig dialog(&theApp->cfgPop, m_fnPopCfg, this);
3 dialog.exec();
4 m_fnPopCfg = dialog.fileName();
5 lbl_fnPopCfg->setText(m_fnPopCfg);
6 }
As the dialog is a local variable, I expect it to be created on stack and destroy immediately once the function ends (after line 5). When the app repeatedly open and close the dialog, its mem usage goes up, and it never return to initial values ['Memory (Working Set)' and 'Memory (Private Working Set)' columns of Task Manager]. I used Application Verifier, enabling all the basic tests, and it shows no error'. The memory pattern looks like follow (numeric values are made-up of illustration only):
Application start (working set = 12000K, private set = 6000K)
Open Dialog-1 (working set = 14000K, private set = 7000K)
Close Dialog (working set = 12010K, private set = 6005K)
Open Dialog-2 (working set = 14020K, private set = 7000K)
Close Dialog (working set = 12010K, private set = 6008K)
Open Dialog-3 (working set = 14080K, private set = 7010K)
Close Dialog (working set = 12040K, private set = 6008K)
...
So, any idea to trace the root cause of the problem? (Actually, I'm also facing similar issue when usage static methods of QFileDialog getOpenFileName, getSaveFileName, and found some discussion here, but it seems not solved)
Edit I use QFormLayout in my dialogs, and I add widgets by layout->addRow("label text", mywidget);, I doubt if the object destruction fail to remove the labels cleanly.
Edit I created a test program with the QDialog have ten QLineEdits, using same add-widget strategy. The problem still exists. (The problem will happen for this test program if I create and close the dialog frequently, says 10 times in a second)
mainwindow.h
#include <QMainWindow>
#include <QPushButton>
#include <QDialog>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
private:
QPushButton * button;
private slots:
void button_click();
};
class Dialog : public QDialog
{
Q_OBJECT
public:
explicit Dialog(QWidget *parent = 0);
};
mainwindow.cpp
#include "mainwindow.h"
#include <QApplication>
#include <QFormLayout>
#include <QLineEdit>
#include <QLabel>
MainWindow::MainWindow(QWidget *parent):QMainWindow(parent)
{
button=new QPushButton(this);setCentralWidget(button);
connect(button,SIGNAL(clicked()),SLOT(button_click()));
}
void MainWindow::button_click()
{
Dialog d(this);
d.exec();
}
Dialog::Dialog(QWidget *parent):QDialog(parent)
{
QFormLayout*layout=new QFormLayout(this);
setLayout(layout);
for (int i = 0; i < 10; i++)
{
layout->addRow(QString("%1").arg(i+1), new QLineEdit(this));
}
}
int main(int c,char *argv[])
{
QApplication a(c,argv);
MainWindow w;
w.show();
return a.exec();
}
Platform
Win 7 x64, MinGW 4.7.2 x64 (rubenvb-build), 4GB ram
Qt 4.8.5 (built natively using above tool-chain)
Qt-Creator 2.6.1 (built natively using above tool-chain)
A few months late, but this might help the next person who comes across this problem. I'm using PySide, but had the same memory leak. There turned out to be two options, depending on what information you need to get back from the dialog:
1) Schedule the dialog for deletion when you're done with it.
In Python, this looks like:
dialog = MyDialog(self)
dialog.exec_()
# Do other things with dialog
dialog.deleteLater()
And it should look similar in your C++ code:
void MainWindow::button_click()
{
Dialog d(this);
d.exec();
// Do other things with d
d.deleteLater()
}
2) Set the WA_DeleteOnClose attribute.
I ended up including this in the custom dialog's constructor:
self.setAttribute(PySide.QtCore.Qt.WA_DeleteOnClose)
Which should look something like this in your C++ code:
Dialog::Dialog(QWidget *parent):QDialog(parent)
{
QFormLayout*layout=new QFormLayout(this);
setLayout(layout);
setAttribute(Qt::WA_DeleteOnClose);
for (int i = 0; i < 10; i++)
{
layout->addRow(QString("%1").arg(i+1), new QLineEdit(this));
}
}
Both of those fixed the memory leak for me, except that it will occasionally leak 4kb if I open/close the dialog very quickly a bunch of times in a row. Sorry for the Python-centric answer -- hopefully this points people in the right direction.
I am developing an TCP server application. I have the newDataReceived slot and I emit a signal in it like this:
void myclass::newDataReceived()
{
char data_received[1024] = {0};
client->read(data_received, client->bytesAvailable());
QString msg = data_received;
QString client_ip = client->peerAddress().toString();
emit dataReceived(msg,client_ip);
}
I have catched the signal from MainWindow, there is no problem. But, I have another class which is a QThread and I want this class to catch this signal too. But it does not do it. I connected the signal to my slot like,
srv_thread = new myclass();
connect(srv_thread, SIGNAL(dataReceived(QString,QString)), this, SLOT(incoming_message(QString,QString)));
What am I missing?
Thanks in advance!
I know similar question to this have been asked, but I haven't found an answer that fixes my problem.
I'm adapting some existing Qt code to add server functionality to a program my company uses. To that end I added a QTcpServer object to the existing dialog, call listen() and connect a slot to the newConnection emitter, like:
.h
class QConsole : public QDialog
{
Q_OBJECT
public:
void init();
public slots:
void new_Connection();
private:
QTcpServer m_Server;
}
.cpp
void QConsole::init()
{
m_Server.listen(QHostAddress::Any, 12346);
QDialog::connect(&m_Server, SIGNAL(newConnection()), this, SLOT(new_Connection()));
}
Main is:
int main( int argc, char *argv[] )
{
QApplication app(argc, argv);
QConsole * _output_window = new QConsole(desktopRect);
_output_window->init();
_output_window->show();
return app.exec();
}
new_Connection() never gets called so I can't see the relevance, but here it is:
void QConsole::new_Connection()
{
}
This works fine in that my program starts listening on the port specified and if I telnet to it a connection of sorts it made, but new_Connection() is never ever ever called!
I've seen posts on this problem dating back to 2005 so it's obviously not a new thing, but what I haven't found is a satisfactory answer to the problem (or any answer actually). This has got everyone at work stumped, even the person that has written a Qt server program. I'm guessing that there is something fundamentally wrong with the existing framework, but I have no idea what it might be.
I have been tearing my hair out for a day and a half over this, and the closes I got to success was using waitForNewConnection() which would actually return me a socket, but when I connected to the readReady() emitter, that was never fired either. So what would prevent these signals never getting called?
Please spare my sanity and help me as much as you can.
Here is a complete working example, tested using MSVC++ 2010.
This listens for a connection on port 12346, replies with "HELLO WORLD" and logs the connection to a list on the dialog.
main.cpp
#include <QtGui>
#include "console.hpp"
int main(int argc, char** argv)
{
QApplication app(argc, argv);
Console con;
con.show();
return app.exec();
}
console.hpp
#include <QtCore>
#include <QtGui>
#include <QtNetwork>
class Console : public QDialog
{
Q_OBJECT
public:
Console();
public slots:
void connection();
private:
QTcpServer mServer;
QListWidget* mConnList;
};
console.cpp
#include "console.hpp"
Console::Console() :
QDialog(),
mServer(),
mConnList(new QListWidget())
{
if (!mServer.listen(QHostAddress::Any, 12346))
qDebug() << "Error during 'listen'" << mServer.errorString();
connect(&mServer, SIGNAL(newConnection()), this, SLOT(connection()));
QVBoxLayout* mainLayout = new QVBoxLayout();
mainLayout->addWidget(mConnList);
setLayout(mainLayout);
}
void Console::connection()
{
qDebug() << "CONNECTION";
QTcpSocket* skt = mServer.nextPendingConnection();
if (!skt)
return;
mConnList->addItem(QString("%1:%2").arg(skt->peerAddress().toString()).arg(skt->peerPort()));
skt->write("HELLO WORLD!\r\n");
skt->close();
}
test.pro
TEMPLATE=app
CONFIG+=console debug
QT=core gui network
HEADERS=console.hpp
SOURCES=main.cpp console.cpp
Another working example, again on Linux, although I have coded a program using QTcpServer to run on both Linux and Windows before without a problem. If this doesn't work, surely it must be either a Qt installation or OS configuration problem. Either that or a bug in the Qt version.
~/tcp_test$ qmake --version
QMake version 2.01a
Using Qt version 4.8.6 in /usr/lib/x86_64-linux-gnu
~/tcp_test$ for file in qconsole.{h,cpp} main.cpp tcp_test.pro ; do echo -e "$file:\n"; cat $file; echo; echo; done
qconsole.h:
#include <QDialog>
#include <QTcpServer>
class QConsole : public QDialog
{
Q_OBJECT
public:
QConsole();
public slots:
void connection();
private:
QTcpServer server;
};
qconsole.cpp:
#include "qconsole.h"
QConsole::QConsole()
{
server.listen(QHostAddress::Any, 12346);
QDialog::connect(&server, SIGNAL(newConnection()), this, SLOT(connection()));
}
void QConsole::connection()
{
qDebug("got connection");
}
main.cpp:
#include <QApplication>
#include "qconsole.h"
int main( int argc, char *argv[] )
{
QApplication app(argc, argv);
QConsole * window = new QConsole();
window->show();
return app.exec();
}
tcp_test.pro:
QT = core gui network
CONFIG += debug
TARGET = tcp_test
SOURCES = main.cpp qconsole.cpp
HEADERS = qconsole.h
~/tcp_test$ ./tcp_test &
[3] 9784
~/tcp_test$ nc localhost 12346
got connection
^C