Function matching in Qt
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::setMainwi开发者_如何学运维ndow(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?
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?
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 ?
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;
};
精彩评论