[SOLVED] problem with two classes in the same file



  • i created a pushbutton in the mainwindow.ui file. in the mainwindow.cpp file, the mainwindow functions, i can access the pushbutton methods. yet in the same mainwindow.cpp file, in the server functions, i can't access the pushbutton methods . i am getting the error in line 54, no matching function for call to 'Ui::MainWindow::setupUi(server* const)'. without the ui->setupUi(this); at line 55, the program crashes. below is the .h and cpp files.

    @
    ---------- mainwindow.h file

    namespace Ui {
    class server;
    }

    namespace Ui {
    class MainWindow;
    }

    class server : public QTcpServer
    {
    Q_OBJECT

    public:
        server(QObject *parent=0);
    
    private:
        Ui::MainWindow *ui;
    

    };

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    private:
    Ui::MainWindow *ui;

    };

    -------------- mainwindow.cpp

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }

    server::server(QObject *parent) :
    QTcpServer(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    ui->textEdit->append("hi");
    }
    @



  • Seems logical to me. You can only setup a ui on a widget, or in this case, on a QMainWindow. QTcpServer is not a QWidget, but a QObject.



  • thanks Andre



  • now that i have two classes in the same file, how to a call the MainWindow function from a server function.

    for example, in the server::server function, i would like to call a function from the MainWindow class. for example...

    @void server::server(){
    MainWindow::Test();
    }@

    but the code above gives an error.



  • "Gives an error" is not very descriptive. You need to post exactly which error you get.

    My guess: Test is not a static method of MainWindow, and thus can not be called on the class (like you do), but needs to be called on an instance of that class. So, you need to make sure that your server class gets a pointer to the mainwindow instance that you created.

    Or, better yet, you read up on the signals and slots mechanism in Qt. That would IMHO be a much better way to communicate between the server and the mainwindow. That way, they do not need pointers to each other, and can be developed independently.



  • i am getting the error: cannot declare member function 'static void MainWindow::test()' to have static linkage.

    i would rather not use signal and slot just yet as that would require about 100+ of them in the program.



  • why did you not simply give to the server a main window pointer:
    @
    class server : public QTcpServer
    {
    Q_OBJECT

    public:
        server(QObject *parent=0);
    
    private:
        MainWindow *mw;
    

    };

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);

    server = new Ui::Server(this);
    

    }

    MainWindow::~MainWindow()
    {
    delete server;
    delete ui;
    }

    server::server(MainWindow *parent) :
    QTcpServer(parent),
    mw(parent)
    {
    mw->textEdit->append("hi");
    }

    server::Test()
    {
    mw->Test();
    }
    @

    If you want to call back the main window use simply the mw member.



  • [quote author="kalster" date="1321427999"]i am getting the error: cannot declare member function 'static void MainWindow::test()' to have static linkage.

    i would rather not use signal and slot just yet as that would require about 100+ of them in the program.[/quote]

    static functions != static linkage

    static functions in C++ are functions that do not access member variables and that can be used due the class scope, not only on an instance.



  • [quote author="BilbonSacquet" date="1321431599"]why did you not simply give to the server a main window pointer:[/quote]

    I think what Andre was saying in the first reply was that the mainwindow is a qwidget and server is an qobject. therefore i get this error when running your code. error: cannot convert 'QObject*' to 'MainWindow*' in initialization.

    I have decided that i am going to use signal and slot. thank you


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.