Results 1 to 2 of 2

Thread: QTreeWidget insert problem

  1. #1
    Join Date
    Jul 2020
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default QTreeWidget insert problem

    I have been trying to insert directory names from the .txt file into treewidget for a while. I managed to access the file from the main.cpp section and find the part I wanted, but I don't know how to import it into the treewidget.

    Main.cpp
    #include "mainwindow.h"
    #include<QApplication>
    #include<QDir>
    #include<QDebug>
    #include<QTreeWidget>

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

    QFile myFile("C:/Users/gozde/Desktop/gözde.txt");
    myFile.open(QIODevice::ReadOnly);
    QTextStream in (&myFile);
    const QString content = in.readLine();

    QString searchString("INCLUDE_DIRECTORIES(");
    QString line;

    do{
    line = in.readLine();
    if (!line.contains(searchString, Qt::CaseSensitive))
    {
    QString one = in.readLine();
    }

    }while(line!=")");

    MainWindow w;
    w.show();
    return a.exec();
    }

    Before understanding what I need to do I am trying to create a treewidget with this ;

    MainWindow.cpp
    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include<QDir>
    #include<QDebug>
    #include<QStack>
    #include<QFileDialog>

    MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    displayTree();
    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }
    void MainWindow::displayTree()
    {
    ui->treeWidget->setColumnCount(1);
    QStringList labels;
    labels<<"TASK1";
    ui->treeWidget->setHeaderLabels(labels);

    QTreeWidgetItem *VmsPtzControl= new QTreeWidgetItem(ui->treeWidget);
    VmsPtzControl->setText(0,"VmsPtzControl");
    VmsPtzControl->setCheckState(0,Qt::Unchecked);
    ui->treeWidget->addTopLevelItem(VmsPtzControl);

    QTreeWidgetItem *Includes= new QTreeWidgetItem();
    Includes->setFlags(Includes->flags() | Qt::ItemIsUserCheckable | Qt::ItemIsSelectable);
    Includes->setCheckState(0,Qt::Unchecked);
    Includes->setText(0,"Includes");
    VmsPtzControl->addChild(Includes);

    QTreeWidgetItem *Cots= new QTreeWidgetItem();
    Cots->setText(0,"Cots");
    Includes->addChild(Cots);
    Cots->setCheckState(0,Qt::Unchecked);

    QTreeWidgetItem *VMS= new QTreeWidgetItem();
    VMS->setText(0,"${VMS}");
    Cots->addChild(VMS);

    QTreeWidgetItem *VMS_U= new QTreeWidgetItem();
    VMS_U->setText(0,"${VMS_U}");
    Cots->addChild(VMS_U);

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,229
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QTreeWidget insert problem

    Qt Code:
    1. QFile myFile("C:/Users/gozde/Desktop/gözde.txt");
    2. myFile.open(QIODevice::ReadOnly);
    3. QTextStream in (&myFile);
    4. const QString content = in.readLine();
    5.  
    6. QString searchString("INCLUDE_DIRECTORIES(");
    7. QString line;
    8.  
    9. do{
    10. line = in.readLine();
    11. if (!line.contains(searchString, Qt::CaseSensitive))
    12. {
    13. QString one = in.readLine();
    14. }
    15.  
    16. } while(line!=")");
    To copy to clipboard, switch view to plain text mode 

    So with this code you are reading your file and doing absolutely nothing with the contents. If you want to add the strings to your tree widget, don't you think it would make more sense to do this in the place where you are actually filling your tree widget?

    If you want to read the file in main(), then the least you could do is to store the strings in a QStringList, then add a method to your MainWindow class that you can call, passing the QStringList as an argument. In that function, you add each string to your tree.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Replies: 2
    Last Post: 14th June 2015, 21:39
  2. Replies: 1
    Last Post: 2nd June 2013, 16:12
  3. SQLITE insert problem
    By rdf in forum Qt Programming
    Replies: 3
    Last Post: 23rd September 2011, 08:13
  4. INSERT query with MySQL problem
    By timmyg in forum Qt Programming
    Replies: 10
    Last Post: 20th March 2008, 22:52
  5. insert Item to my QTreeWidget
    By raphaelf in forum Qt Programming
    Replies: 7
    Last Post: 28th February 2006, 16:42

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.