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.

    mainwindow.h

    @#ifndef MAINWINDOW_H
    #define MAINWINDOW_H

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

    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

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

    };

    #endif // MAINWINDOW_H
    @

    main.cpp
    @#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);
    ui->treeView->setModel(dirmodel)
    
    w.show();
    
    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
    ui->treeView->setModel(dirmodel)
    ^@

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


  • Moderators

    Hi,

    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
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.