PDA

View Full Version : QTreeWidget insert problem



avery96
24th July 2020, 09:10
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);

d_stranz
24th July 2020, 16:48
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!=")");

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.