Congratulations to our 2022 Qt Champions!

Not Declared In This Scope

  • I'm trying to write a super basic file manager with QFileSystemModel, but I had barely started before running into compilation errors.


    @#ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>
    #include <QtCore>
    #include <QtGui>
    #include <QFileSystemModel>

    namespace Ui {
    class MainWindow;

    class MainWindow : public QMainWindow

    explicit MainWindow(QWidget *parent = 0);

    Ui::MainWindow *ui;
    QFileSystemModel *dirmodel;
    QFileSystemModel *filemodel;


    #endif // MAINWINDOW_H

    @#include "mainwindow.h"
    #include <QApplication>

    int main(int argc, char *argv[])
    QApplication a(argc, argv);
    MainWindow w;

    QString sPath = "$HOME/";
    dirmodel = new QFileSystemModel(this);
    dirmodel-> setRootPath(sPath);
    return a.exec&#40;&#41;;


    Debugger Output
    @-1: In function 'int main(int, char**)':
    10: error: 'dirmodel' was not declared in this scope
    dirmodel = new QFileSystemModel(this);
    ^10: error: invalid use of 'this' in non-member function
    dirmodel = new QFileSystemModel(this);
    ^12: error: 'ui' was not declared in this scope

    Why does it give me these errors? is it something I forgot to include?

  • Moderators


    ui is a private member variable of the MainWindow class. You can only access it from one of MainWindow's functions; you cannot access it from the main() function.

  • Ah I made a mistake, this was supposed to be in mainwindow.cpp, not main.cpp.

    Thanks :)

    QFileSystemModel doesn't seem to correctly extract icon information from my system so it's only using generic icons for folders and drives (I have this issue with other QT applications that use this class as well)

    How do I make it properly read the icon theme that I am using for other applications? I am on Linux.

Log in to reply