PDA

View Full Version : Problem calling a function of a class from another class.



Fallen_
2nd September 2010, 17:55
Hi, when calling these functions (the highlighted ones (between [.b][./b])):

#include "startup.h"
#include <QtGui>

Startup::Startup(QWidget* parent)
: QDialog(parent)
{
portLabel = new QLabel(tr("Port:"));
chanLabel = new QLabel(tr("Channel:"));
nickLabel = new QLabel(tr("Nick:"));
serverLabel = new QLabel(tr("Server:"));

portLine = new QLineEdit;
chanLine = new QLineEdit;
nickLine = new QLineEdit;
serverLine = new QLineEdit;

done = new QPushButton(tr("Finished"));
quit = new QPushButton(tr("Close"));

mainW = new MainWindow;

connect(done, SIGNAL(clicked()), this, SLOT(showMainWindow()));
connect(quit, SIGNAL(clicked()), this, SLOT(close()));

QHBoxLayout* v = new QHBoxLayout;
v->addWidget(serverLabel);
v->addWidget(serverLine);
v->addWidget(portLabel);
v->addWidget(portLine);
v->addWidget(chanLabel);
v->addWidget(chanLine);
v->addWidget(nickLabel);
v->addWidget(nickLine);
v->addWidget(done);
v->addWidget(quit);
setLayout(v);
resize(100, 80);
setWindowTitle(tr("Startup Window"));
}

void Startup::showMainWindow()
{
mainW->show();
}

QString Startup::getServer() {
return serverLine->text();
}

QString Startup::getNick() {
return nickLine->text();
}

QString Startup::getChannel() {
return chanLine->text();
}

QString Startup::getPort() {
return portLine->text();
}

they return either "" or 0
what am i doing wrong?

Zlatomir
2nd September 2010, 18:10
Make sure that you initialize your member variables before you call those functions.

If you want to read(return) what the user write in the QLineEdit (just a guess) you should make them slots (and connect them with signals)
Documentation for signals and slots here (http://doc.trolltech.com/4.6/signalsandslots.html)

Fallen_
2nd September 2010, 18:20
nope, didnt work.

tbscope
2nd September 2010, 18:25
Can you give an example of how and where you call these functions?

By the way, such functions are best made const.

Fallen_
2nd September 2010, 19:00
sure, i also knew that someone will say that theyre made const, i had them this way.
anyway here:

void MainWindow::Connect()
{
Startup s;
QString p = s.getPort();
int port = p.toInt();
QString host = s.getServer();
socket->connectToHost(host, port);
QString t = s.getNick();
std::string tmp = "USER " + t.toStdString() + " * * :" + t.toStdString() + "\r\n";
socket->write(tmp.c_str());
tmp = "NICK " + t.toStdString() + "\r\n";
socket->write(tmp.c_str());
QString f = s.getChannel();
tmp = "JOIN " + f.toStdString() + "\r\n";
socket->write(tmp.c_str());
}
i also tried putting "Startup* s" then new it in the constructor didnt work, since i called both classes in both headers.

tbscope
2nd September 2010, 19:12
Ahh, this will not work. And it's a little bit weird too.

You have a dialog. This dialog creates a main window when done. Then in the main window, you create the dialog again.
I suggest you do this a little bit different.

Now, Startup is a dialog where you can set settings. In your connect function, you create a Startup object called s.

Startup s;
So far that is ok.

But in your next line, you already ask for a setting.

QString p = s.getPort();

You will get the initialised version of that setting here. Do you know why?
You never show the dialog, not modeless or modal. So you never set any setting. You just created the dialog object.

I suggest you remove the creation of the main window from the dialog.
Start with the main window right away.

Then from within your main window, set some slots to receive the data like setData(server, port, username, password).
In your dialog create a signal to send the data like newData(server, port, username, password).

In your connect function (for example), you can do this:


Startup s;
connect(&s, SIGNAL(newData(server, ...)), this, SLOT(setData(server, ...)));
s.exec();

In your dialog slot for the done button (in your case called showMainWindow(), emit the signal.

Fallen_
2nd September 2010, 19:20
can u show me how the signal should be like (i mean the function itself)

tbscope
2nd September 2010, 19:42
An example in pseudo code (I didn't check it for syntactical errors).

mymainwindow.h

class MyMainWindow : public QMainWindow
{
Q_OBJECT

public
explicit MyMainWindow(...);
~MyMainWindow();

public slots: //or protected
void connect();
void connectTo(const QString &server, const QString &port, const QString &nick, const QString &channel);
};

mymainwindow.cpp


MyMainWindow::MyMainWindow(...) :
QMainWindow(...)
{
// Create your controls, like a menu, buttons, etc...

connect(connectAction /*or button*/, SIGNAL(clicked()), this, SLOT(connect()));
}

void MyMainWindow::connect()
{
MyConnectDialog connectDialog;

connect(&connectDialog, SIGNAL(newConnection(QString, QString, QString, QString)), this, SLOT(connectTo(QString, QString, QString, QString)));

connectDialog.exec();
}

void MyMainWindow::connectTo(const QString &server, const QString &port, const QString &nick, const QString &channel)
{
// Do the actual connection here based on the settings.
}


myconnectdialog.h

class MyConnectDialog : public QDialog
{
Q_OBJECT

public:
explicit MyConnectDialog(...);
~MyConnectDialog();

protected slots:
void makeConnection();

signals:
void newConnection(const QString &server, const QString &port, const QString &nick, const QString &channel);
};

myconnectdialog.cpp

MyConnectDialog::MyConnectDialog(...) :
QDialog(...)
{
...
connect(done, SIGNAL(clicked()), this, SLOT(makeConnection()));
...
}

void MyConnectDialog::makeConnection()
{
emit newConnection(serverLine->text(), ...);
}

I hope this is clear.