Qt not locating slots in simple example program - qt

This is a simple example that fails to run. The run-time errors are:
QObject::connect: No such slot Bridge::setText(const QString& text)
QObject::connect: No such slot QLabel::setText(const QString& text)
I have two files, main.cpp and Bridge.h.
main.cpp
#include <QtWidgets>
#include "Bridge.h"
int main(int argc, char ** argv)
{
QApplication app (argc, argv);
QWidget widget;
QLineEdit * lineEdit = new QLineEdit;
QLabel * label = new QLabel;
QVBoxLayout * layout = new QVBoxLayout;
layout->addWidget(lineEdit);
layout->addWidget(label);
widget.setLayout(layout);
Bridge * bridge = new Bridge ("", &app);
QObject::connect(
lineEdit, SIGNAL(textChanged(const QString &)),
bridge, SLOT(setText(const QString & text))
);
QObject::connect(
bridge, SIGNAL(textChanged(const QString &)),
label, SLOT(setText(const QString & text))
);
widget.show();
return app.exec();
}
Bridge.h
#include <QString>
#include <QObject>
class Bridge : public QObject
{
Q_OBJECT
public:
Bridge(const QString & text, QObject * parent = 0) : QObject(parent), text(text) {}
const QString & getText() const;
int getLengthOfText() const;
public slots:
void setText(const QString & text) {
if (this->text == text) {
return;
}
this->text = text;
emit textChanged(text);
}
signals:
void textChanged(const QString &);
private:
QString text;
};
.pro file
######################################################################
# Automatically generated by qmake (3.0) Sat May 17 20:28:07 2014
######################################################################
TEMPLATE = app
TARGET = qtbox
INCLUDEPATH += .
INCLUDEPATH += headers
QT += widgets
# Input
HEADERS += headers/Bridge.h
SOURCES += sources/main.cpp
######################################################################
# Build to custom directories
######################################################################
release: DESTDIR = build/release
debug: DESTDIR = build/debug
OBJECTS_DIR = $$DESTDIR/.obj
MOC_DIR = $$DESTDIR/.moc
RCC_DIR = $$DESTDIR/.qrc
UI_DIR = $$DESTDIR/.ui
Am I missing something obvious here? Some random things I can come up with:
Do I need to define setText in a .cpp file? If so, why would I have to do this?
Is whitespace a problem? ie QString& vs QString &
Am I missing something in my qmake file?

No, you do not. But it is common to do so, especially if you need to change your implementation; you will not have to recompile all files that include that header file.
No, both are valid.
No, also, the qmake file seems valid.
Change your connect calls not to have a variable, but only the type:
connect(bridge, SIGNAL(textChanged(QString)), label, SLOT(setText(QString)));
Edit: Also, #include <QLabel> to eventually run qmake manually once.

Related

Qt: QObject::connect: No such slot QObject::processPendingDatagrams() [duplicate]

This question already has answers here:
When should Q_OBJECT be used?
(4 answers)
QT "No such slot" Error [duplicate]
(1 answer)
Closed 6 years ago.
I have written an UDP programme with Qt, and when I connect this:
connect(socket,SIGNAL(readyRead()),this,SLOT(processPendingDatagrams()));
the complier tells me that
no such slot
the error click here
and I want to know how to fix it, thank you!
P.S.
Here are my files:
files
Here are my codes:
enter code here
udptest.cpp:
#include "udptest.h"
#include <QObject>
#include <QUdpSocket>
#include <QtNetwork>
UDPtest::UDPtest()
{
socket = new QUdpSocket();
port = 2016;
socket->bind(port,QUdpSocket::ShareAddress
| QUdpSocket::ReuseAddressHint);
connect(socket,SIGNAL(readyRead()),this,SLOT(processPendingDatagrams()));
}
QString UDPtest::getIP()
{
QList<QHostAddress> list = QNetworkInterface::allAddresses();
foreach (QHostAddress address, list)
{
if(address.protocol() == QAbstractSocket::IPv4Protocol)
return address.toString();
}
return 0;
}
void UDPtest::sendMessage(QString message)
{
QByteArray data;
QDataStream out(&data,QIODevice::WriteOnly);
QString localHostName = QHostInfo::localHostName();
QString address = getIP();
out <<"123"<< localHostName << address << message;
socket->writeDatagram(data,data.length(),QHostAddress::Broadcast, port);
}
void UDPtest::processPendingDatagrams()
{qDebug()<<"receive";
while(socket->hasPendingDatagrams())
{
QByteArray datagram;
datagram.resize(socket->pendingDatagramSize());
socket->readDatagram(datagram.data(),datagram.size());
QDataStream in(&datagram,QIODevice::ReadOnly);
QString userName,localHostName,ipAddress,message;
QString time = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
in >>userName >>localHostName >>ipAddress >>message;
QString msg=time+userName+localHostName+ipAddress+message;
msger=msg;
qDebug()<<msg;
}
}
QString UDPtest:: messager()
{
return msger;
}
main.cpp:
#include"udptest.h"
#include<QDebug>
#include <QtCore/QCoreApplication>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
qDebug()<<"123";
UDPtest test;
test.sendMessage("aha");
return a.exec();
}
udptest.h:
#ifndef UDPTEST_H
#define UDPTEST_H
#include <QObject>
#include <QUdpSocket>
#include <QtCore/QCoreApplication>
#include <QtNetwork>
class UDPtest:public QObject
{
public:
UDPtest();
QString messager();
void sendMessage(QString);
private slots:
void processPendingDatagrams();
private:
QString msger;
QUdpSocket *socket;
qint16 port;
QString getIP();
};
#endif // UDPTEST_H
QudptestConsole.pro:
QT += core
QT -= gui
QT += network
CONFIG += c++11
TARGET = QudptestConsole
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp \
udptest.cpp
HEADERS += \
udptest.h
You have forgotten Q_OBJECT macro in UDPtest class
class UDPtest: public QObject
{
Q_OBJECT
public:
UDPtest();
.....
}

Cannot read output using readyReadStandardOutput in Qt

I am using Qt and I am unable to get output of a exe file using readyReadStandardOutput.
Here is my code.
mainwindow.cpp
void MainWindow::on_pushButton_24_clicked()
{
myprocess = new QProcess(this);
myprocess->start("files\\helloworld.exe");
connect(myprocess, SIGNAL(readyReadStandardOutput ()), this, SLOT(outlog()));
}
void MainWindow::outlog()
{
QString abc;
abc = myprocess->readAllStandardOutput();
emit outlogtext(abc);
ui->lineEdit_4->setText(abc);
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QtGui>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
QProcess *myprocess;
signals:
void outlogtext(QString ver);
private slots:
void outlog();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
The helloworld.exe will just give a output "Hello world" but I cannot see it in the textEdit, whats wrong with my code? I am very new to Qt. Thank you
I got the program working. Below is the code.
mainwindow.hpp
#ifndef MAINWINDOW_HPP
#define MAINWINDOW_HPP
#include <QtGui>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
signals:
void outlogtext(QString ver);
private slots:
void outlog();
void on_pushButton_24_clicked();
private:
QPushButton* pushButton_24;
QLineEdit* lineEdit_4;
QProcess *myprocess;
};
#endif // MAINWINDOW_HPP
main.cpp
#include <QtCore>
#include <QtGui>
#include <QDebug>
#include "mainwindow.hpp"
MainWindow::MainWindow(QWidget* parent)
: QMainWindow(parent)
{
pushButton_24 = new QPushButton;
connect(pushButton_24, SIGNAL(clicked()),
this, SLOT(on_pushButton_24_clicked()));
lineEdit_4 = new QLineEdit;
QWidget* central = new QWidget;
QLayout* layout = new QVBoxLayout();
layout->addWidget(pushButton_24);
layout->addWidget(lineEdit_4);
central->setLayout(layout);
setCentralWidget(central);
}
MainWindow::~MainWindow()
{
}
void MainWindow::on_pushButton_24_clicked()
{
myprocess = new QProcess(this);
connect(myprocess, SIGNAL(readyReadStandardOutput()),
this, SLOT(outlog()));
myprocess->start("./helloworld.exe");
// For debugging: Wait until the process has finished.
myprocess->waitForFinished();
qDebug() << "myprocess error code:" << myprocess->error();
}
void MainWindow::outlog()
{
QString abc = myprocess->readAllStandardOutput();
emit outlogtext(abc);
lineEdit_4->setText(abc);
}
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
MainWindow win;
win.show();
return app.exec();
}
helloworld.cpp
#include <iostream>
int main()
{
std::cout << "Hello World!" << std::endl;
}
Some things I changed:
After constructing an object, I always connect signals and slots before performing the
actual operation on the object, which might be calling show() for widgets or calling
start() for threads. So I can be sure that I do not miss a signal like started(),
for example.
I ran the program on Linux. There I had to make sure that helloworld.exe was on my
path and I changed the command to ./helloworld.exe. I didn't create the sub-directory
called files as in your example.
The character to separate directories in Qt is the slash /. There are special functions to convert between the Qt-style and the native-style, when you want to display something to the user. Internally always make use of slashes. This even works for Windows programs (many console commands can cope with a slash instead of a backslash, too).
Adding debug output is really, really valuable during development. If the Makefile is
not set up correctly or something breaks, the helloworld.exe might end up in a directory, where it is not expected. Thus, I added code to wait for some time until the process has finished. This does not hurt because helloworld.exe needs just some milliseconds to run. Afterwards, I print the error code of QProcess just to be sure that the program has been found and could be executed. So I can be sure that the executable is on my path, that the executable flag is set, that I have permissions for executing the file etc.
I don't know exactly what causes the problem on your machine. However, comparing your solution to mine, having a look at the error code of QProcess and setting break points inside the slots should help you finding the error.

Simple QT console TCP application. What am I doing wrong?

#include <QtCore/QCoreApplication>
#include <QTCore>
#include <QtNetwork>
#include <QDebug>
#define CONNECT(sndr, sig, rcvr, slt) connect(sndr, SIGNAL(sig), rcvr, SLOT(slt))
class mynet : QObject
{
Q_OBJECT
public:
mynet()
{}
void start()
{
CONNECT(tcpServer, newConnection(), this, acceptConnection());
CONNECT(tcpClient, connected(), this, startTransfer());
CONNECT(tcpClient, bytesWritten(qint64), this, updateClientProgress(qint64));
CONNECT(tcpClient, error(QAbstractSocket::SocketError), this, displayError(QAbstractSocket::SocketError));
// start server listening
tcpServer->listen();
while(!tcpServer->isListening());
// make client connection
tcpClient->connectToHost(QHostAddress::LocalHost, tcpServer->serverPort());
}
public slots:
void acceptConnection()
{
tcpServerConnection = tcpServer->nextPendingConnection();
CONNECT(tcpServerConnection, readyRead(), this, updateServerProgress());
CONNECT(tcpServerConnection, error(QAbstractSocket::SocketError), this, displayError(QAbstractSocket));
tcpServer->close();
}
void startTransfer()
{
bytesToWrite = TotalBytes - (int)tcpClient->write(QByteArray(PayloadSize, '#'));
}
void updateServerProgress()
{
bytesReceived += (int)tcpServerConnection->bytesAvailable();
tcpServerConnection->readAll();
if (bytesReceived == TotalBytes)
{
qDebug() << "done";
tcpServerConnection->close();
}
}
void updateClientProgress(qint64 numBytes)
{
// callen when the TCP client has written some bytes
bytesWritten += (int)numBytes;
// only write more if not finished and when the Qt write buffer is below a certain size.
if (bytesToWrite > 0 && tcpClient->bytesToWrite() <= 4*PayloadSize)
bytesToWrite -= (int)tcpClient->write(QByteArray(qMin(bytesToWrite, PayloadSize), '#'));
}
void displayError(QAbstractSocket::SocketError socketError)
{
if (socketError == QTcpSocket::RemoteHostClosedError)
return;
qDebug() << tcpClient->errorString();
tcpClient->close();
tcpServer->close();
}
private:
QTcpServer* tcpServer;
QTcpSocket* tcpClient;
QTcpSocket* tcpServerConnection;
int bytesToWrite;
int bytesWritten;
int bytesReceived;
int TotalBytes;
int PayloadSize;
};
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
mynet m1;
m1.start();
return a.exec();
}
I get an
Undefined symbols for architecture x86_64:
"vtable for mynet", referenced from:
mynet::mynet() in main.o
mynet::~mynet()in main.o.
Please advise what I am doing wrong. Can I not inline the method definitions in the class for some reason in Qt?
You need to add your class to the .pro file
HEADERS += mynet.h
SOURCES += mynet.cpp
so the meta-object compiler can scan them and work out they need moc'ing and generate the relevant stubs.
Assuming that your source file is named foo.cpp, you have to put the following line at the very end:
#include "foo.moc"
This line tells qmake and the VS Qt add-in that the file should be run via moc, and that the generated moc file should be named foo.moc.
You also have problems in the #include lines for Qt headers. I've found that the following work:
#include <QtCore/QCoreApplication>
#include <QtNetwork/QTcpServer>
#include <QtNetwork/QTcpSocket>
Make sure to add network to your .pro file. This will create the correct linking to the network library functions.
QT += core network
Two things:
1) You should publicly derive from QObject.
2) Are you moc'ing this file and then compiling and linking the output? If you include the Q_OBJECT macro and don't moc, you will get an error like that.

Why my project doesn't link if there are Q_OBJECT macros in .cpp files?

This code compiles, links and works as intended:
#include <QApplication>
#include <QListView>
#include "File_List_Model.h"
int main(int c,char**v)
{
QApplication app(c,v);
QStringList list;
list << "a" << "b" << "c";
File_List_Model* model = new File_List_Model;
model->set_entries(list);
QListView* view = new QListView;
view->setModel(model);
view->show();
return app.exec();
}
But when I put the class definition in .cpp file instead of header files, I get linker errors stating that vtable was not properly defined.
#include <QApplication>
#include <QListView>
//#include "File_List_Model.h"
#include "File_List_Proxy.h"
#include <QAbstractItemModel>
#include <QStringList>
class File_List_Model : public QAbstractItemModel
{
Q_OBJECT
private:
QStringList data_;
public:
File_List_Model(QObject *parent = 0) :
QAbstractItemModel(parent)
{
}
int columnCount(const QModelIndex& parent) const
{
return 1;
}
QVariant data(const QModelIndex & index, int role) const
{
switch(role)
{
case Qt::DisplayRole:
return data_[index.row()];
default:
return QVariant();
}
}
QModelIndex index(int row, int column, const QModelIndex & parent) const
{
return createIndex(row,column);
}
QModelIndex parent(const QModelIndex & index) const
{
return QModelIndex();
}
bool set_entries(const QStringList& entries)
{
if (entries.size())
{
beginInsertRows(createIndex(0,0),0,entries.size());
data_ = entries;
endInsertRows();
emit dataChanged(createIndex(0,0),createIndex(0,entries.size()));
return true;
}
else
{
return false;
}
}
int rowCount(const QModelIndex & parent) const
{
return data_.size();
}
};
int main(int c,char**v)
{
QApplication app(c,v);
QStringList list;
list << "a" << "b" << "c";
File_List_Model* model = new File_List_Model;
model->set_entries(list);
File_List_Proxy* proxy = new File_List_Proxy;
proxy->setSourceModel(model);
QListView* view = new QListView;
view->setModel(proxy);
view->show();
return app.exec();
}
//error:
debug/moc_File_List_Model.o:moc_File_List_Model.cpp:(.rdata$_ZTV15File_List_Model[vtable for File_List_Model]+0x44): undefined reference to `File_List_Model::columnCount(QModelIndex const&) const'
debug/moc_File_List_Model.o:moc_File_List_Model.cpp:(.rdata$_ZTV15File_List_Model[vtable for File_List_Model]+0x4c): undefined reference to `File_List_Model::data(QModelIndex const&, int) const'
This seems to be exactly the same code. Why does it link when the code is in headers and it doesn't link otherwise?
Qt uses moc tool to handle C++ extensions that a required, for example, for signals-slots mechanism. This tool processes all header (!) files in the project and generates new source files that contain meta-object code for those classes that contain Q_OBJECT macro.
When you have your class defined in .cpp file instead of .h file moc fails to process it properly.
Have a look at this article for more information about Qt Meta-Object Compiler.
Because Qt runs moc on header files and don't run it on sources.
The linker is complaining about missing the object code that comes from compiling moc output. That's because although moc has processed the source file, its output was not compiled into an object file.
For header files, the build system assumes that they are meant for inclusion in multiple translation units and won't violate the one definition rule. Thus moc output can include the header file, and get compiled as a stand-alone translation unit.
But, if you have any Q_OBJECT macros within a .cpp file, the moc output cannot be compiled in isolation: it won't have access to the declarations from your .cpp file and thus can't compile! It also can't include your .cpp file, since that would violate the one definition rule: two translation units - moc output and your .cpp file - would define the same stuff.
Instead, you need to append moc's output to the end of the .cpp file. E.g., if you have O_OBJECT in main.cpp, add #include "main.moc" at the end of the file:
// main.cpp
#include <QtCore>
struct Object : QObject {
Q_OBJECT
};
int main() {
Object o;
qDebug() << o.metaObject()->className();
}
#include "main.moc"
// "main.moc" depends on the declaration of Object above!
The above is an SSCCE.
One could argue that perhaps qmake/cmake should set up the build so that moc output is automatically appended to the .cpp file before it's sent to the compiler. So far, that feature is not implemented.

File not found during compilation

What is wrong with the code bellow? When I compile it I get a warning that file not found. Something is invalid. I'm probably making a few mistakes here. I think the problem is perhaps with the way I inherit from QWidget.
#include <QtGui/QApplication>
#include "filedialogs.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
FileDialogs w;
w.openFile();
return 0;
}
#ifndef FILEDIALOGS_H
#define FILEDIALOGS_H
#include <QWidget>
class QFileDialog;
class FileDialogs : public QWidget
{
public:
FileDialogs(QWidget *parent = 0);
~FileDialogs();
void openFile();
};
#endif // FILEDIALOGS_H
#include <QFileDialog>
#include "filedialogs.h"
FileDialogs::FileDialogs(QWidget *parent)
: QWidget(parent)
{
}
FileDialogs::~FileDialogs()
{
}
void FileDialogs::openFile()
{
QString filename = QFileDialog::getOpenFileName(
this,
tr("Open Document"),
QDir::currentPath(),
tr("Document files (*.doc *.rtf);;All files (*.*)") );
if( !filename.isNull() )
{
qDebug( filename.toAscii() );
}
}
#-------------------------------------------------
#
# Project created by QtCreator 2011-07-29T19:06:33
#
#-------------------------------------------------
QT += core gui
TARGET = exX
TEMPLATE = app
SOURCES += main.cpp\
filedialogs.cpp
HEADERS += filedialogs.h
This error message is emitted by the MOC compiler. You are missing the Q_OBJECT macro. Put it in your class declaration like this:
class FileDialogs : public QWidget
{
Q_OBJECT
public:
....
I know this Question is very old. But in my case it was another problem.
I had to include the path of the headers manually in the .pro file.
INCLUDEPATH += src/subdir

Resources