I'm trying to make a simple test to use an UI object made with "Qt Design" but I'm pretty new to Qt and C++. I've got a quite simple Ui : 3 LineEdit
s and 1 PushButton
: IMAGE : the UI window
I've a Client Class which is supposed to control Ui. It connects the QPushButton
and it should get the content from QLineEdit
. But the result in QDebug
is always the same when I push the Button, even when I change QlineEdit
field: "Client connected : "" : 0 "
Moreover, if I use on_pushButton_clicked
made with QtDesign, it will display the real values of QlineEdit
s.
Why the QString
s are always the same ? Am I passing a copy of the initial object ? How to solve that ?
Is it the good way to make a ViewController
? Else, what is the good way?
Client.cpp
#include "client.h"
#include "mainwindow.h"
#include "logwindow.h"
Client::Client()
{
LogWindow* w1 = new LogWindow();
MainWindow* w2 = new MainWindow();
_stack = new QStackedWidget();
_stack->addWidget(w1);
connect(w1->getButton(),SIGNAL(clicked()),this,SLOT(connexion()));
_stack->addWidget(w2);
_stack->show();
}
//When the button is Pushed, gets the content from QlineEdits and prints them
void Client::connexion()
{
QString ip=(LogWindow (_stack->currentWidget())).getIP();
int port=((LogWindow (_stack->currentWidget())).getPort()).toInt();
socket = new QTcpSocket(this);
socket->connectToHost(ip, port);
_stack->setCurrentIndex((_stack->currentIndex()+1)%_stack->count());
qDebug() <<"Client connected : " << ip << ":"<<port;
}
And a class made automatically by Qt :
LogWindow.cpp
#include "logwindow.h"
#include "ui_logwindow.h"
LogWindow::LogWindow(QWidget *parent) :
QWidget(parent),
ui(new Ui::LogWindow)
{
ui->setupUi(this);
}
LogWindow::~LogWindow()
{
delete ui;
}
QPushButton* LogWindow::getButton()
{
return ui->pushButton;
}
QString LogWindow::getIP()
{
//LineEdit named "IP_text"
return ui->IP_text->text();
}
QString LogWindow::getPort()
{
//LineEdit named "Port_text"
return ui->Port_text->text();
}
LogWindow.h
namespace Ui {
class LogWindow;
}
class LogWindow : public QWidget
{
Q_OBJECT
public:
explicit LogWindow(QWidget *parent = 0);
~LogWindow();
QPushButton* getButton();
QString getIP();
QString getPort();
private slots:
void on_pushButton_clicked();
private:
Ui::LogWindow *ui;
};
Thuga solved it :
In Client::connexion you are creating a new instance of LogWindow. Make LogWindow* w1 a member variable of your Client class, if you want to access it in other Client's member functions as well.
There is not much to complain about, except that _stack is a widget without a parent, so you must make sure you destroy it when you don't need it anymore (for example call delete _stack; in the destructor). Most beginners would have tried to make the ui variable public to get the data from IP_text, but you did correctly, by making the LogWindow::getIP function.
If you don't want to expose ui->pushButton outside of your class, you can make a signal for your LogWindow class, and connect the clicked signal of ui->pushButton to that signal. You can connect signals to signals, it doesn't have to be a slot.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.