Results 1 to 4 of 4

Thread: Qtree in C++

  1. #1
    Join Date
    Jun 2008
    Posts
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Qtree in C++

    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 ???


    Qt Code:
    1. #include <QtGui>
    2. #include "treemodelcompleter.h"
    3. #include "mainwindow.h"
    4.  
    5. MainWindow::MainWindow(QWidget *parent)
    6. : QMainWindow(parent), completer(0), lineEdit(0)
    7. {
    8. createMenu();
    9.  
    10. completer = new TreeModelCompleter(this);
    11. completer->setModel(modelFromFile(":/resources/treemodel.txt"));
    12. completer->setSeparator(QLatin1String("1"));
    13. QObject::connect(completer, SIGNAL(highlighted(const QModelIndex&)),
    14. this, SLOT(highlight(const QModelIndex&)));
    15.  
    16. QWidget *centralWidget = new QWidget;
    17.  
    18. QLabel *modelLabel = new QLabel;
    19. modelLabel->setText(tr("Tree Model<br>(Double click items to edit)"));
    20.  
    21.  
    22.  
    23. QLabel *separatorLabel = new QLabel;
    24. separatorLabel->setText(tr("Tree Separator"));
    25.  
    26. QLineEdit *separatorLineEdit = new QLineEdit;
    27. separatorLineEdit->setText(completer->separator());
    28. connect(separatorLineEdit, SIGNAL(textChanged(const QString&)),
    29. completer, SLOT(setSeparator(const QString&)));
    30.  
    31. QCheckBox *wrapCheckBox = new QCheckBox;
    32. wrapCheckBox->setText(tr("Wrap around completions"));
    33. wrapCheckBox->setChecked(completer->wrapAround());
    34. connect(wrapCheckBox, SIGNAL(clicked(bool)), completer, SLOT(setWrapAround(bool)));
    35.  
    36. contentsLabel = new QLabel;
    37. contentsLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    38. connect(separatorLineEdit, SIGNAL(textChanged(const QString&)),
    39. this, SLOT(updateContentsLabel(const QString&)));
    40.  
    41. treeView = new QTreeView;
    42. treeView->setModel(completer->model());
    43. treeView->header()->hide();
    44. treeView->expandAll();
    45.  
    46. lineEdit = new QLineEdit;
    47. lineEdit->setCompleter(completer);
    48.  
    49. QGridLayout *layout = new QGridLayout;
    50. layout->addWidget(modelLabel, 0, 0); layout->addWidget(treeView, 0, 1);
    51.  
    52. layout->addWidget(separatorLabel, 3, 0); layout->addWidget(separatorLineEdit, 3, 1);
    53. layout->addWidget(wrapCheckBox, 4, 0);
    54. layout->addWidget(contentsLabel, 5, 0, 1, 2);
    55. layout->addWidget(lineEdit, 6, 0, 1, 2);
    56. centralWidget->setLayout(layout);
    57. setCentralWidget(centralWidget);
    58.  
    59. setWindowTitle(tr("Tree Model Completer"));
    60. lineEdit->setFocus();
    61. }
    62.  
    63. void MainWindow::createMenu()
    64. {
    65. QAction *exitAction = new QAction(tr("Exit"), this);
    66.  
    67.  
    68. connect(exitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
    69.  
    70.  
    71. QAction *openAct = new QAction(tr("Open"),this);
    72.  
    73. connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
    74.  
    75. QMenu* fileMenu = menuBar()->addMenu(tr("File"));
    76.  
    77.  
    78. fileMenu ->addAction(openAct);
    79.  
    80. fileMenu->addAction(exitAction);
    81.  
    82. }
    83. void MainWindow::open()
    84. {
    85. QString fileName =
    86. QFileDialog::getOpenFileName(this, tr("Open Bookmark File"),
    87. QDir::currentPath() );
    88.  
    89. }
    90.  
    91.  
    92.  
    93. QAbstractItemModel *MainWindow::modelFromFile(const QString& fileName)
    94. {
    95. QFile file(fileName);
    96.  
    97. if (!file.open(QFile::ReadOnly))
    98.  
    99. return new QStringListModel(completer);
    100.  
    101. QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
    102. QStringList words;
    103.  
    104. QStandardItemModel *model = new QStandardItemModel(completer);
    105. QVector<QStandardItem *> parents(10);
    106. parents[0] = model->invisibleRootItem();
    107.  
    108. while (!file.atEnd())
    109. {
    110.  
    111. QString line = file.readLine();
    112. QString trimmedLine = line.trimmed();
    113.  
    114. if (line.isEmpty() || trimmedLine.isEmpty())
    115. continue;
    116.  
    117. QRegExp re("^\\s+");
    118.  
    119. int nonws = re.indexIn(line);
    120. int level = 0;
    121.  
    122. if (nonws == -1)
    123. {
    124. level = 0;
    125. }
    126. else {
    127. if (line.startsWith("\t"))
    128. {
    129. level = re.cap(0).length();
    130. }
    131. else
    132. {
    133. level = re.cap(0).length()/4;
    134. }
    135. }
    136.  
    137. if (level+1 >= parents.size())
    138. parents.resize(parents.size()*2);
    139.  
    140. item->setText(trimmedLine);
    141. parents[level]->appendRow(item);
    142. parents[level+1] = item;
    143. }
    144.  
    145. QApplication::restoreOverrideCursor();
    146.  
    147. return model;
    148. }
    149.  
    150.  
    151.  
    152.  
    153.  
    154. void MainWindow::updateContentsLabel(const QString& sep)
    155. {
    156. contentsLabel->setText(QString(tr("Type path from model above with items at each level separated by a '%1'")).arg(sep));
    157. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by jpn; 1st July 2008 at 09:12. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qtree in C++

    Quote Originally Posted by rrrrcem View Post
    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.

  3. #3
    Join Date
    Jun 2008
    Posts
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Qtree in C++

    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???

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qtree in C++

    Quote Originally Posted by rrrrcem View Post
    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.

    Quote Originally Posted by rrrrcem View Post
    do I need to define some algorithms in these methods too???
    I'm not sure what you mean.

Similar Threads

  1. Hover and Highlight QTable and QTree Items
    By VireX in forum Qt Programming
    Replies: 41
    Last Post: 18th May 2007, 22:55

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.