Search code examples
c++qtqt-creator

Qt: class Ui::StartScreen has no member named 'lbl_gif'


I have read many other forums, but this is not the result of a typo or missing definition in the .ui file, and thus I am at a loss. I added the label in the Designer pane of QtCreator, then went over to the .cpp file to reference the label and add a movie. That did not work so I tried adding just text, and still no luck. I have cleaned the project, rebuilt it, closed and reopened Qt, deleted and re-added new labels with new names, but Qt is not recognizing any labels I add to this form. All other forms work fine when adding labels. I know the label is there because the intellisense autocompletes the lbl_gif name for me, and it is present in the dropdown of options after typing "ui->".

Error:

C:\Users\user\OneDrive\Desktop\project\screens\startscreen.cpp:30: error: 'class Ui::StartScreen' has no member named 'lbl_gif'
..\screens\startscreen.cpp: In constructor 'StartScreen::StartScreen(QWidget*)':
..\screens\startscreen.cpp:30:9: error: 'class Ui::StartScreen' has no member named 'lbl_gif'
   30 |     ui->lbl_gif->setText("testing");
      |         ^~~~~~~~~~~~~~~~~~~~~~~

StartScreen class constructor where lbl_gif is referenced:

// Constructor
StartScreen::StartScreen(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::StartScreen)
{
    // Setup the mainwindow
    ui->setupUi(this);
    setWindowTitle("title");

     // Load logo
    QString path = R"(C:/Users/user/OneDrive/Desktop/project/logos/8bitlogo.png)";
    QPixmap img(path);
    img = img.scaled(img.size()*.9, Qt::KeepAspectRatio);
    ui->lbl_logo->setPixmap(img);

    // Load gif
    QMovie *movie = new QMovie("C:/Users/user/OneDrive/Desktop/project/logos/knight.gif");
    ui->lbl_gif->setText("testing");
    movie->start();

    this->show();
}

StartScreen.ui snippet proving existence of object in ui:

      <layout class="QVBoxLayout" name="verticalLayout_6" stretch="0,2,2,2,2,2">
       <item>
        <widget class="QLabel" name="lbl_gif">
         <property name="text">
          <string>TextLabel</string>
         </property>
        </widget>
       </item>

Solution

  • I just did as @musicamante suggested and ran C:/Qt/6.6.1/mingw_64/bin/uic.exe .\startscreen.ui from the directory containing the form files, which created a fresh ui_startscreen.h file. I opened this file and confirmed it contained the line QLabel *lbl_gif; under public with the rest of the member variables. The project was still having the build errors.

    By chance I noticed that an outdated version of the ui_startscreen.h file had somehow made its way into the project directory where the .pro file was also located. Thus, it was reading the outdated ui_startscreen.h file instead of reading the one in the build folder. The problem has been resolved.