tags:

views:

119

answers:

3

Hello, I have some trouble with Qt.

I have a class 'Core'

class Core {

   public:

        static QString get_file_content(QString filename);
        static void setMainwindow(Ui::MainWindow const *w);

   private:
        static MainWindow *main_window;
};

and class 'MainWindow' in namespace Ui:

namespace Ui {
    class MainWindow;
}

class MainWindow : public QMainWindow {
    Q_OBJECT
public:
    MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
};

In MainWindow constructor I make

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

and gets error

mainwindow.cpp:8: error: no matching function for call to 'Core::setMainwindow(MainWindow* const)'

Of cource, i include core.h with declaration of 'Core' class.

That's occurs only on setMainwindow method.

So the questions is - why Core class method setMainwindow() is invisible in MainWindow class?

+1  A: 

Hey,

Did you add "Core.h" in your MainWindow's cpp/h file ?

Did you try without parameter in setMainWindow, just to check if it's not something related to it ?

Edit : Yeah seems to me you need MainWindow as parameter, not Ui::MainWindow, don't you think ?

Andy M
Yes, of course i add. Didn't help.
Alexander
Try and change your MainWindow class's name... Try to remove/change your parameter in your setMainWindow... I'm sure it's something around that...
Andy M
+4  A: 

The problem is that your Core::setMainwindow method takes a Ui::MainWindow* and you are passing a MainWindow*. From the code you posted you have two MainWindow classes, one in the namespace Ui and one in the top-level namespace. Is this what you mean or should there only be the one in Ui perhaps?

Troubadour
There is one class MainWindow, that declared in Ui namespace. But even if i pass *ui as an argument to setMainwindow i get same error
Alexander
@Alexander: Not in the code you've posted. In that code there are two distinct `MainWindow` classes. One is a declaration only of a `MainWindow` class in the `Ui` namespace and the other is a full definition of a `MainWindow` class in the top-level namespace.
Troubadour
@Alexander: BTW, if you only want one `MainWindow` class then what is the reasoning behind the constructor instantiating another main window? You realise that'll give you an infinite loop that will eventually exhaust memory if you try to run it?
Troubadour
@alexander: you shouldn't pass *ui as an argument, just ui. then you shouldn't get an error. @Troubadour, there is no infinite loop. The constructor instantiates Ui::MainWindow instead of MainWindow.
sisis
+1  A: 

Your MainWindow class isn't nested inside the Ui namespace. You forward-declared the Ui::MainWindow class, but then implemented a separate ::MainWindow class (in the global namespace). Because of this, your Core::setMainwindow takes a Ui::MainWindow but you're passing a ::MainWindow.

I'm guessing that this lack of nesting is correct -- and Ui::MainWindow is generated by Qt Designer, and MainWindow is the implementation class that contains all of your custom code. If so, change your code to:

class Core {

   public:

        static QString get_file_content(QString filename);
        static void setMainwindow(MainWindow const *w);

   private:
        static MainWindow *main_window;
};
Ken Bloom
That will mean he's left with an infinite loop. But at least it'll compile!
Troubadour
@Troubadour I don't see any infinite loop here. `Ui::MainWindow` has all of the widgets, but none of the code for how they interact. `::MainWindow` has all of the code for how they interact. It's the standard Qt design pattern when using Qt Designer to draw your interface.
Ken Bloom
@Ken: I was commenting on your initial answer and code, not the version that now appears before me.
Troubadour
@Troubadour -- sorry.
Ken Bloom