Allocation of incomplete type "Ui::MainWindow"
-
I am getting error in initializing the ui pointer in my MainWindow which I think is automatically generated code. I am Using qt 6. and Here my Code :
MainWindow.cpp
#include "ui_mainwindow.h" #include "SortFactory.h" #include "SortObject.h" #include <QListView> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); init(); } MainWindow::~MainWindow() { delete ui; } void MainWindow::init() { ui->comboType->setView(new QListView(this)); ui->comboType->addItems(SortFactory::getInstance()->getSortList()); connect(ui->btnSort, &QPushButton::clicked, this, [this]{ const int type = ui->comboType->currentIndex(); if (type != ui->canvas->getSortType()) { SortObject *obj = SortFactory::getInstance()->createSortObject(type, ui->canvas); ui->canvas->setSortObject(type, obj); } ui->canvas->sort(ui->spinCount->value(), ui->spinInterval->value()); }); connect(ui->btnStop, &QPushButton::clicked, this, [this]{ ui->canvas->stop(); }); connect(ui->canvas, &MainCanvas::runFlagChanged, this, [this](bool running){ ui->comboType->setEnabled(!running); ui->spinCount->setEnabled(!running); ui->spinInterval->setEnabled(!running); ui->btnSort->setEnabled(!running); }); }
I am getting the main error in the initializer list hence anywhere *ui is used also giving error.
-
Take a look into your generated file "ui_mainwindow.h" and you will see that you do not named your widget "MainWindow" but something else. I would guess "mainwindow".
-
@Christian-Ehrlicher I have mainwindow.h in which I have this MainWindow class here is the code for that.
#define MAINWINDOW_H #include <QMainWindow> QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } QT_END_NAMESPACE class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); private: void init(); private: Ui::MainWindow *ui; }; #endif // MAINWINDOW_H
-
@Aahi Please read again what @Christian-Ehrlicher suggested: "Take a look into your generated file "ui_mainwindow.h" and you will see that you do not named your widget "MainWindow" but something else."
-
@jsulm @Christian-Ehrlicher Thank you so much It is now working I did the following steps in case anyone in the future got the same error.
-
I checked the
ui_mainwindow.h
file in that the class innamespace Ui
wasMainUI
so I changed the iterator fromUi::mainwindow"
toUi::MainUI
. -
I still was getting the error but this time it was "cannot change the iterator from
Ui::mainwindow
toUi::MainUI
and this was due to the reason that inmainwindow.h
theclass MainWindow
has already declared the variable asUi::mainwindow *ui
so I changed it toUi::MainUI *ui
. -
But again got the error
No type named "MainUI" in namespace "Ui"
and solution was very simple just includeui_mainwindow.h
in that and everything is now runnning perfectly fine.
Again thank you so much to @Christian-Ehrlicher and @jsulm
-