PDA

View Full Version : Qtree in C++


rrrrcem
1st July 2008, 08:35
I need to create something like Qtree ı did most of the parts , it reads the file and creates the qtree but my problem is, how can ı add extra items to this qtree,, ı mean how can ı make it bigger ???


#include <QtGui>
#include "treemodelcompleter.h"
#include "mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), completer(0), lineEdit(0)
{
createMenu();

completer = new TreeModelCompleter(this);
completer->setModel(modelFromFile(":/resources/treemodel.txt"));
completer->setSeparator(QLatin1String("1"));
QObject::connect(completer, SIGNAL(highlighted(const QModelIndex&)),
this, SLOT(highlight(const QModelIndex&)));

QWidget *centralWidget = new QWidget;

QLabel *modelLabel = new QLabel;
modelLabel->setText(tr("Tree Model<br>(Double click items to edit)"));



QLabel *separatorLabel = new QLabel;
separatorLabel->setText(tr("Tree Separator"));

QLineEdit *separatorLineEdit = new QLineEdit;
separatorLineEdit->setText(completer->separator());
connect(separatorLineEdit, SIGNAL(textChanged(const QString&)),
completer, SLOT(setSeparator(const QString&)));

QCheckBox *wrapCheckBox = new QCheckBox;
wrapCheckBox->setText(tr("Wrap around completions"));
wrapCheckBox->setChecked(completer->wrapAround());
connect(wrapCheckBox, SIGNAL(clicked(bool)), completer, SLOT(setWrapAround(bool)));

contentsLabel = new QLabel;
contentsLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
connect(separatorLineEdit, SIGNAL(textChanged(const QString&)),
this, SLOT(updateContentsLabel(const QString&)));

treeView = new QTreeView;
treeView->setModel(completer->model());
treeView->header()->hide();
treeView->expandAll();

lineEdit = new QLineEdit;
lineEdit->setCompleter(completer);

QGridLayout *layout = new QGridLayout;
layout->addWidget(modelLabel, 0, 0); layout->addWidget(treeView, 0, 1);

layout->addWidget(separatorLabel, 3, 0); layout->addWidget(separatorLineEdit, 3, 1);
layout->addWidget(wrapCheckBox, 4, 0);
layout->addWidget(contentsLabel, 5, 0, 1, 2);
layout->addWidget(lineEdit, 6, 0, 1, 2);
centralWidget->setLayout(layout);
setCentralWidget(centralWidget);

setWindowTitle(tr("Tree Model Completer"));
lineEdit->setFocus();
}

void MainWindow::createMenu()
{
QAction *exitAction = new QAction(tr("Exit"), this);


connect(exitAction, SIGNAL(triggered()), qApp, SLOT(quit()));


QAction *openAct = new QAction(tr("Open"),this);

connect(openAct, SIGNAL(triggered()), this, SLOT(open()));

QMenu* fileMenu = menuBar()->addMenu(tr("File"));


fileMenu ->addAction(openAct);

fileMenu->addAction(exitAction);

}
void MainWindow::open()
{
QString fileName =
QFileDialog::getOpenFileName(this, tr("Open Bookmark File"),
QDir::currentPath() );

}



QAbstractItemModel *MainWindow::modelFromFile(const QString& fileName)
{
QFile file(fileName);

if (!file.open(QFile::ReadOnly))

return new QStringListModel(completer);

QApplication::setOverrideCursor(QCursor(Qt::WaitCu rsor));
QStringList words;

QStandardItemModel *model = new QStandardItemModel(completer);
QVector<QStandardItem *> parents(10);
parents[0] = model->invisibleRootItem();

while (!file.atEnd())
{

QString line = file.readLine();
QString trimmedLine = line.trimmed();

if (line.isEmpty() || trimmedLine.isEmpty())
continue;

QRegExp re("^\\s+");

int nonws = re.indexIn(line);
int level = 0;

if (nonws == -1)
{
level = 0;
}
else {
if (line.startsWith("\t"))
{
level = re.cap(0).length();
}
else
{
level = re.cap(0).length()/4;
}
}

if (level+1 >= parents.size())
parents.resize(parents.size()*2);

QStandardItem *item = new QStandardItem;
item->setText(trimmedLine);
parents[level]->appendRow(item);
parents[level+1] = item;
}

QApplication::restoreOverrideCursor();

return model;
}





void MainWindow::updateContentsLabel(const QString& sep)
{
contentsLabel->setText(QString(tr("Type path from model above with items at each level separated by a '%1'")).arg(sep));
}

jacek
2nd July 2008, 03:48
how can ı add extra items to this qtree,, ı mean how can ı make it bigger ???
Exactly the same way as you do it in MainWindow::modelFromFile() --- use QStandardItem's and QStandardItemModel's insert*() or append*() methods.

rrrrcem
2nd July 2008, 10:55
jacek Thanks for your suggestion

but there is another problem.. there is not only one insert or append method..

there are insertRow and insertColumnd and also appendRow and appendColumn..

Do ı need to use both row and column methods and also

do I need to define some algorithms in these methods too???

jacek
12th July 2008, 23:39
Do ı need to use both row and column methods
If you set the number of columns with QStandardItemModel::setColumnCount(), you will have to add only rows.

do I need to define some algorithms in these methods too???
I'm not sure what you mean.