PDA

View Full Version : Undeclared Identifier from ui_ header



JediSpam
25th August 2011, 00:19
I am currently trying to fill a QTreeView and having some issues with my mainwindow.cpp file seeing the definition of the tree in ui_mainwindow.h. Here are the pieces of code I feel should let this variable be defined:

from ui_mainwindow.h (generated)


treeView_test = new QTreeView(groupBox);
treeView_test->setObjectName(QString::fromUtf8("treeView_test"));
treeView_test->setGeometry(QRect(80, 320, 211, 471));
from mainwindow.cpp


treeView_test->setModel(standardModel);
treeView_test->expandAll();
from mainwindow.h


#include "ui_mainwindow.h"

class QTreeView;

I am receiving the following error in mainwindow.cpp (the first location treeView_test is set)

error 2065: 'treeView_test' : undeclared identifier

Why can't mainwindow.cpp see treeView_test from ui_mainwindow.h? any ideas? Thanks!

stampede
25th August 2011, 00:28
You need to create an instance of Ui::MainWindowUi class (or whatever is the name in ui_mainWindow.h file), then call setupUi( ) to actually create the ui elements. This is usually done in your custom class' constructor:


// .h
#include "ui_mainWindow.h"

class MainWindow: public QMainWindow
{
//...
protected:
Ui::MainWindowUi ui;
};
// .cpp
MainWindow::MainWindow( QWidget * parent ) : QMainWindow(parent){
ui.setupUi(this);
}

Then you can use the treeView:


void MainWindow::doSomething(){
ui.treeView_test->...
}

JediSpam
25th August 2011, 16:14
thanks! i was forgetting the ui. in front of all the treeView declarations!