Qt cant modify a label into de ui - qt

I am creating a popuo window that can change the message that shows. I have the next class
class NoPutPort : public QDialog, public Ui::NoPortPut
{
Q_OBJECT;
public:
NoPutPort(QWidget *parent=0) {
setupUi(this);
}
~NoPutPort(void) {}
void putPort(QString a){
ui.label_2->setText(a);
}
private:
Ui::NoPortPut ui;
};
The problem if when I call the method putPort, the application crash and I dont know why. If I put ui.label_2, it dont crash, but when I access to the object to modify it, it crash.
Anyone knows how can I modify the label correctly?

You've messed up the code. It should be:
class NoPutPort : public QDialog
{
Q_OBJECT;
public:
NoPutPort(QWidget *parent=0) {
ui.setupUi(this);
}
~NoPutPort(void) {}
void putPort(QString a){
ui.label_2->setText(a);
}
private:
Ui::NoPortPut ui;
};
XOR
class NoPutPort : public QDialog, public Ui::NoPortPut
{
Q_OBJECT;
public:
NoPutPort(QWidget *parent=0) {
setupUi(this);
}
~NoPutPort(void) {}
void putPort(QString a){
label_2->setText(a);
}
};

Related

C++ method defined in .h called in .cpp is not recognized

I am trying to use a method called getName() that is defined in the addPlayer.h file below. The getname() method will be used in the constructor called Player. The error that I get is: 'getName' was not declared in this scope'. How can I fix it?
Thanks for helping.
The addPlayer.h file:
#include "../Source/Player.h"
class addPlayer : public QDialog
{
Q_OBJECT
public:
addPlayer(QWidget *parent = 0);
~addPlayer();
tp::Player* makePlayer();
void addPlayer();
QString getName() const;
inline QString addPlayer::getName() const
{
return (ui.name_lineEdit->text());
}
The addPlayer.cpp file:
#include "addPlayer.h"
#include <QMessageBox>
using namespace tp;
addPlayer::addPlayer(QWidget *parent)
: QDialog(parent)
{
ui.setupUi(this);
QObject::connect(ui.ok_pushButton, SIGNAL(clicked()), this,
SLOT(validatePlayer()));
QObject::connect(ui.dateNaissance_pushButton, SIGNAL(clicked()), this,
SLOT(getDate()));
}
addPlayer::~addPlayer()
{
}
Player* makePlayer()
{
return new Player(getName().toStdString());
}
The constructor is as follows:
class Player: public HumanBeing {
public:
//Constructor
Player(const std::string& p_name);
Player::Player(const std::string& p_name): m_name(p_name)
You haven't properly scoped your definition of makePlayer -- you need to prefix addPlayer:::
Player* addPlayer::makePlayer()
{
return new Player(getName().toStdString());
}

QObject::connect in QRunnable - console

I create simple multi threading server:
Create server
If new connection create new QThreadpool - QRunnable
In runnable send message to client and wait request
If client was been disconnected runnable write qDebug and runnable quit.
server.h
class Server : public QTcpServer
{
Q_OBJECT
public:
explicit Server(QObject *parent = 0);
void StartServer();
protected:
void incomingConnection(int handle);
private:
QThreadPool *pool;
};
server.cpp:
#include "server.h"
Server::Server(QObject *parent) :
QTcpServer(parent)
{
pool = new QThreadPool(this);
pool->setMaxThreadCount(10);
}
void Server::StartServer()
{
this->listen(QHostAddress(dts.ipAddress),80));
}
void Server::incomingConnection(int handle)
{
Runnable *task = new Runnable();
task->setAutoDelete(true);
task->SocketDescriptor = handle;
pool->start(task);
}
runnable.h
class Runnable : public QRunnable
{
public:
Runnable();
int SocketDescriptor;
protected:
void run();
public slots:
void disconnectCln();
};
runnable.cpp
#include "runnable.h"
Runnable::Runnable()
{
}
void Runnable::run()
{
if(!SocketDescriptor) return;
QTcpSocket *newSocketCon = new QTcpSocket();
newSocketCon->setSocketDescriptor(SocketDescriptor);
!!! How make it???!!! QObgect::connect(newSocketCon, SIGNAL(disconnected()), this, SLOTS(disconnectCln()));
newSocketCon->write(mes.toUtf8().data());
newSocketCon->flush();
newSocketCon->waitForBytesWritten();
}
void Runnable::disconnectCln()
{
qDebug() << "Client was disconnect";
}
You seem to have neglected to actually ask a question, but here's the immediate problem I spot with your code: Your Runnable class does not inherit from QObject, and thus cannot have signals and slots. You will need to do that to have any hope of making it work.
class Runnable : public QObject, public QRunnable
{
Q_OBJECT
public:
Runnable();
int SocketDescriptor;
protected:
void run();
public slots:
void disconnectCln();
};
There are two important things to note here. 1) if you use multiple inheritance, QObject must come first in the list. 2) To use signals and slots you must include the Q_OBJECT macro in your class definition.

Cross thread issue in Qt4.8

I have this code:
class CMyWindow:
class CMyWindow: public QMainWindow
{ // Q_OBJECT .... here
private:
CMyServer *server;
public:
CMyWindow(QWidget *parent): QMainWindow(parent)
{
// Setup GUI here
server = new CMyServer(this);
server->startServer();
}
void thisChangeLabelCaption(QString str) {
ui.lblStatus.setText(str);
}
}
And class CMyServer:
class CMyServer: public QTcpServer
{
protected:
void incomingConnection(int sockDesc) {
/* Why below line can't be done :-| */
((CMyWindow *) parent())->thisChangeLabelCaption("New connection");
}
}
But the line in incomingConnection() rountine seem not executed.
Please tell me solution for this problem.
Update:
As #vtmarvin said, i tried this way:
class CMyWindow: public QMainWindow
{
private:
CMyServer *server;
protected slots:
void editLabel(QString str) {
thisChangeLabelCaption(str);
}
public:
CMyWindow(QWidget *parent): QMainWindow(parent) {
server = new CMyServer(this);
server->startServer();
}
void thisChangeLabelCaption(QString str) {
ui.lblStatus.setText(str);
}
}
class CMyServer: public QTcpServer
{
Q_SIGNAL:
void setText(QString str);
protected:
void incomingConnection(int sockDesc) {
/* Why below line can't be done :-| */
emit setText("New connection");
}
public:
CMyServer(QObject *parent): QTcpServer(parent)
{
connect(this, SIGNAL(setText(QString)), parent, SLOT(editLabel(QString)), Qt::QueuedConnection);
}
}
But no better result :-(
You cannot change UI from other threads than the main one - the one that owns QMainWindow. I suppose your CMyServer::incomingConnection is invoked by the QTcpServer thread. You must do a signal-slot with Qt::QueuedConnection type.

Qt signal/slot connection does not work

I am having a very strange problem with QObject::connect method. First please take a look at this very straightforward code:
class B : public QWidget {
Q_OBJECT
public:
explicit B(QWidget* parent = 0) : QWidget(parent) { }
signals:
void event();
}
class A : public QObject {
Q_OBJECT
public:
explicit A(QWidget* parent = 0) : QObject(parent) { b = new B(parent); init(); }
void init() { QObject::connect(b, SIGNAL(event()), this, SLOT(handler())); }
public slots:
void handler() { /*spit out some text*/ }
private:
B* b;
}
An object of A does not respond to signals emitted from object of B. I am confident that the signal is emitted as expected. The QObject::connect method return true indicating success. I ran qmake, moc and the moc_.cpp* files seems correct.
I wonder where my did I make mistake?
Edit I:
Here is the code I am working on, it is stripped down so only the relevant parts are shown:
class ListController : public QObject {
Q_OBJECT
public:
explicit ListController(Model* model, QWidget* parent = 0) : QObject(parent) { compositeView = new CompositeView(parent); initConnections(); }
void initConnections() { QObject::connect(compositeView->getListView(), SIGNAL(event()), this, SLOT(handler())); }
public slots:
void handler() { qDebug()<<"signal is received ..."; }
private:
CompositeView* view;
};
class CompositeView: public QGroupBox {
Q_OBJECT
public:
explicit CompositeView(QWidget* parent = 0) : QGroupBox(parent) { listView = new ListView(this); }
ListView* getListView() const { return listView; }
private:
ListView* listView;
};
class ListView : public QListWidget {
Q_OBJECT
public:
explicit ListView(QWidget* parent = 0) : QListWidget(parent) { }
protected:
void dropEvent(QDropEvent *event) { emit signal(); }
signals:
void signal();
};
I create a new ListController object inside a QWidget subclass passing itself as a parent and providing an appropriate Model object.
Edit II
The ListController returns CompositeView object to the main widget. the main widget adds the the composite view to its layout. At this point the parent of the CompositeView and its children is changed. Which might be the source of the problem.
The answer of this problem was way much easier than I expected.
I think I made a mistake of doing the following steps:
ListController is created on the stack.
The CompositeView object is returned and added to the main widget layout.
ListController object goes silently out of scope and gets destroyed and consequently the connection.
Comment 13 from the top was actually the solution. Thanks a lot tmpearce for your advice.
singals:
void signal();
I doubt, if it the actual code you are working, then please check the typo error.

Qt4 parent pointer usage

I'm having some problem with understanding usage of parent pointer in QT4.
class firstClass : public QWidget
{
Q_OBJECT
public:
firstClass(QWidget *parent = 0);
~firstClass();
void doSomething();
private:
secondClass * myClass;
};
class secondClass : public QWidget
{
Q_OBJECT
public:
secondClass(QWidget *parent = 0);
void doSomethingElse();
};
I want to call doSomething() method while running doSomethingElse(). Is there any way to do it using parent pointer?
I tried parent->doSomething() but it doesn't work. It seems that Qt Creator is suggesting only methods from QObject class after parent->.
On the other hand I can't write it like secondClass(firstClass *parent = 0); - compilator returns error:
Thanks for any suggestions.
If you are positive that the parent of secondClass is always going to be firstClass then you can do this:
static_cast<firstClass *>(parent)->doSomething();
Alternatively you can use qobject_cast and check to make sure that parent is actually an instance of firstClass:
firstClass *myParent = qobject_cast<firstClass *>(parent);
if(myParent){
myParent->doSomething();
}
The more Qt-ish way to do this would be to use signals and slots, instead of trying to directly call a different function.
class firstClass : public QWidget
{
Q_OBJECT
public:
firstClass(QWidget *parent = 0);
~firstClass();
public slot:
void doSomething();
private:
secondClass * myClass;
};
class secondClass : public QWidget
{
Q_OBJECT
public:
secondClass(QWidget *parent = 0);
void doSomethingElse()
{
// ...
emit ( triggerDoSomething() );
// ...
}
signal:
void triggerDoSomething();
};
firstClass::firstClass(QWidget *parent) :
QWidget(parent), myClass(new secondClass(this))
{
// ...
bool connected = connect(myClass, SIGNAL(triggerDoSomething()),
SLOT(doSomething()));
assert( connected );
}

Resources