Results 1 to 8 of 8

Thread: Problem calling a function of a class from another class.

  1. #1
    Join Date
    Aug 2010
    Posts
    58
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Problem calling a function of a class from another class.

    Hi, when calling these functions (the highlighted ones (between [.b][./b])):
    Qt Code:
    1. #include "startup.h"
    2. #include <QtGui>
    3.  
    4. Startup::Startup(QWidget* parent)
    5. : QDialog(parent)
    6. {
    7. portLabel = new QLabel(tr("Port:"));
    8. chanLabel = new QLabel(tr("Channel:"));
    9. nickLabel = new QLabel(tr("Nick:"));
    10. serverLabel = new QLabel(tr("Server:"));
    11.  
    12. portLine = new QLineEdit;
    13. chanLine = new QLineEdit;
    14. nickLine = new QLineEdit;
    15. serverLine = new QLineEdit;
    16.  
    17. done = new QPushButton(tr("Finished"));
    18. quit = new QPushButton(tr("Close"));
    19.  
    20. mainW = new MainWindow;
    21.  
    22. connect(done, SIGNAL(clicked()), this, SLOT(showMainWindow()));
    23. connect(quit, SIGNAL(clicked()), this, SLOT(close()));
    24.  
    25. v->addWidget(serverLabel);
    26. v->addWidget(serverLine);
    27. v->addWidget(portLabel);
    28. v->addWidget(portLine);
    29. v->addWidget(chanLabel);
    30. v->addWidget(chanLine);
    31. v->addWidget(nickLabel);
    32. v->addWidget(nickLine);
    33. v->addWidget(done);
    34. v->addWidget(quit);
    35. setLayout(v);
    36. resize(100, 80);
    37. setWindowTitle(tr("Startup Window"));
    38. }
    39.  
    40. void Startup::showMainWindow()
    41. {
    42. mainW->show();
    43. }
    44.  
    45. [b]QString Startup::getServer() {
    46. return serverLine->text();
    47. }
    48.  
    49. QString Startup::getNick() {
    50. return nickLine->text();
    51. }
    52.  
    53. QString Startup::getChannel() {
    54. return chanLine->text();
    55. }
    56.  
    57. QString Startup::getPort() {
    58. return portLine->text();
    59. }[/b]
    To copy to clipboard, switch view to plain text mode 
    they return either "" or 0
    what am i doing wrong?
    Last edited by Fallen_; 2nd September 2010 at 20:00.

  2. #2
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Problem calling a function of a class from another class.

    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

  3. #3
    Join Date
    Aug 2010
    Posts
    58
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem calling a function of a class from another class.

    nope, didnt work.

  4. #4
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Problem calling a function of a class from another class.

    Can you give an example of how and where you call these functions?

    By the way, such functions are best made const.

  5. #5
    Join Date
    Aug 2010
    Posts
    58
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem calling a function of a class from another class.

    sure, i also knew that someone will say that theyre made const, i had them this way.
    anyway here:
    Qt Code:
    1. void MainWindow::Connect()
    2. {
    3. Startup s;
    4. QString p = s.getPort();
    5. int port = p.toInt();
    6. QString host = s.getServer();
    7. socket->connectToHost(host, port);
    8. QString t = s.getNick();
    9. std::string tmp = "USER " + t.toStdString() + " * * :" + t.toStdString() + "\r\n";
    10. socket->write(tmp.c_str());
    11. tmp = "NICK " + t.toStdString() + "\r\n";
    12. socket->write(tmp.c_str());
    13. QString f = s.getChannel();
    14. tmp = "JOIN " + f.toStdString() + "\r\n";
    15. socket->write(tmp.c_str());
    16. }
    To copy to clipboard, switch view to plain text mode 
    i also tried putting "Startup* s" then new it in the constructor didnt work, since i called both classes in both headers.
    Last edited by Fallen_; 2nd September 2010 at 20:07.

  6. #6
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Problem calling a function of a class from another class.

    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.
    Qt Code:
    1. Startup s;
    To copy to clipboard, switch view to plain text mode 
    So far that is ok.

    But in your next line, you already ask for a setting.
    Qt Code:
    1. QString p = s.getPort();
    To copy to clipboard, switch view to plain text mode 

    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:

    Qt Code:
    1. Startup s;
    2. connect(&s, SIGNAL(newData(server, ...)), this, SLOT(setData(server, ...)));
    3. s.exec();
    To copy to clipboard, switch view to plain text mode 

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

  7. #7
    Join Date
    Aug 2010
    Posts
    58
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem calling a function of a class from another class.

    can u show me how the signal should be like (i mean the function itself)
    Last edited by Fallen_; 2nd September 2010 at 20:24.

  8. #8
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Problem calling a function of a class from another class.

    An example in pseudo code (I didn't check it for syntactical errors).

    mymainwindow.h
    Qt Code:
    1. class MyMainWindow : public QMainWindow
    2. {
    3. Q_OBJECT
    4.  
    5. public
    6. explicit MyMainWindow(...);
    7. ~MyMainWindow();
    8.  
    9. public slots: //or protected
    10. void connect();
    11. void connectTo(const QString &server, const QString &port, const QString &nick, const QString &channel);
    12. };
    To copy to clipboard, switch view to plain text mode 

    mymainwindow.cpp
    Qt Code:
    1. MyMainWindow::MyMainWindow(...) :
    2. {
    3. // Create your controls, like a menu, buttons, etc...
    4.  
    5. connect(connectAction /*or button*/, SIGNAL(clicked()), this, SLOT(connect()));
    6. }
    7.  
    8. void MyMainWindow::connect()
    9. {
    10. MyConnectDialog connectDialog;
    11.  
    12. connect(&connectDialog, SIGNAL(newConnection(QString, QString, QString, QString)), this, SLOT(connectTo(QString, QString, QString, QString)));
    13.  
    14. connectDialog.exec();
    15. }
    16.  
    17. void MyMainWindow::connectTo(const QString &server, const QString &port, const QString &nick, const QString &channel)
    18. {
    19. // Do the actual connection here based on the settings.
    20. }
    To copy to clipboard, switch view to plain text mode 

    myconnectdialog.h
    Qt Code:
    1. class MyConnectDialog : public QDialog
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. explicit MyConnectDialog(...);
    7. ~MyConnectDialog();
    8.  
    9. protected slots:
    10. void makeConnection();
    11.  
    12. signals:
    13. void newConnection(const QString &server, const QString &port, const QString &nick, const QString &channel);
    14. };
    To copy to clipboard, switch view to plain text mode 

    myconnectdialog.cpp
    Qt Code:
    1. MyConnectDialog::MyConnectDialog(...) :
    2. QDialog(...)
    3. {
    4. ...
    5. connect(done, SIGNAL(clicked()), this, SLOT(makeConnection()));
    6. ...
    7. }
    8.  
    9. void MyConnectDialog::makeConnection()
    10. {
    11. emit newConnection(serverLine->text(), ...);
    12. }
    To copy to clipboard, switch view to plain text mode 

    I hope this is clear.

Similar Threads

  1. class calling other class functions?
    By Hardstyle in forum Newbie
    Replies: 4
    Last Post: 2nd June 2010, 03:38
  2. QtConncurrent - calling function within the class
    By jacek_ in forum Qt Programming
    Replies: 5
    Last Post: 28th October 2009, 18:37
  3. Replies: 1
    Last Post: 30th March 2009, 17:07
  4. Replies: 3
    Last Post: 16th May 2007, 12:07
  5. Problems calling C function in C++/Qt class
    By Rayven in forum General Programming
    Replies: 2
    Last Post: 2nd June 2006, 22:32

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.