Skip to content

Instantly share code, notes, and snippets.

@ialexpovad
Last active June 22, 2023 06:12
Show Gist options
  • Save ialexpovad/2fdf86b001eb7d42ea8041ceff5d9cdb to your computer and use it in GitHub Desktop.
Save ialexpovad/2fdf86b001eb7d42ea8041ceff5d9cdb to your computer and use it in GitHub Desktop.
To open a certain HTML file and display its contents to the right of the QTreeView when an item is selected, you can use a QPlainTextEdit or QTextBrowser widget. Here's an example of how you can achieve this:
// CustomModel.cpp
#include "custommodel.h"
CustomModel::CustomModel(QObject *parent)
: QAbstractItemModel(parent), m_rootItem(nullptr)
{
// Create the root item
m_rootItem = new TreeItem("Root");
// Populate the model with data
TreeItem* parentItem = m_rootItem;
TreeItem* item1 = new TreeItem("findfile", parentItem);
TreeItem* item2 = new TreeItem("index", parentItem);
parentItem->addChild(item1);
parentItem->addChild(item2);
TreeItem* childItem1 = new TreeItem("Child Item 1", item1);
TreeItem* childItem2 = new TreeItem("Child Item 2", item1);
item1->addChild(childItem1);
item1->addChild(childItem2);
}
QModelIndex CustomModel::index(int row, int column, const QModelIndex &parent) const
{
if (!hasIndex(row, column, parent))
return QModelIndex();
TreeItem *parentItem = getItem(parent);
TreeItem *childItem = parentItem->child(row);
if (childItem)
return createIndex(row, column, childItem);
return QModelIndex();
}
QModelIndex CustomModel::parent(const QModelIndex &child) const
{
if (!child.isValid())
return QModelIndex();
TreeItem *childItem = getItem(child);
TreeItem *parentItem = childItem->parent();
if (parentItem == m_rootItem)
return QModelIndex();
return createIndex(parentItem->row(), 0, parentItem);
}
int CustomModel::rowCount(const QModelIndex &parent) const
{
TreeItem *parentItem = getItem(parent);
if (parentItem)
return parentItem->childCount();
return 0;
}
int CustomModel::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return 1; // Assuming only one column for the name
}
QVariant CustomModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
if (role != Qt::DisplayRole)
return QVariant();
TreeItem *item = getItem(index);
return item->data(index.column());
}
TreeItem* CustomModel::getItem(const QModelIndex &index) const
{
if (index.isValid()) {
TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
if (item)
return item;
}
return m_rootItem;
}
// CustomModel.h
#include <QAbstractItemModel>
#include "TreeItem.h"
class CustomModel : public QAbstractItemModel
{
Q_OBJECT
public:
explicit CustomModel(QObject *parent = nullptr);
QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
QModelIndex parent(const QModelIndex &child) const override;
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
int columnCount(const QModelIndex &parent = QModelIndex()) const override;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
private:
TreeItem* m_rootItem;
TreeItem* getItem(const QModelIndex &index) const;
};
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
// MainWindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "form.h"
#include <QFile>
#include <QDockWidget>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
//Form *w = new Form(ui->widget);
// QObject::connect(w, &Form::textsignal, [this](QString str) {ui->plainTextEdit->appendPlainText(str);});
// Create the custom model
model = new CustomModel(this);
// Create the QTreeView
treeView = new QTreeView(this);
treeView->setModel(model);
// Create the QPlainTextEdit
// Create the QTextBrowser
textEdit = new QTextBrowser(this); textEdit->setReadOnly(true);
// Create the QDockWidget and set its contents
dockWidget = new QDockWidget(tr("HTML Content"), this);
dockWidget->setWidget(textEdit);
// Set the dock widget as the right dock widget
addDockWidget(Qt::RightDockWidgetArea, dockWidget);
// Connect the item selection signal to the slot
connect(treeView, &QTreeView::pressed, this, &MainWindow::handleItemSelection);
// Set the central widget as the tree view
setCentralWidget(treeView);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::handleItemSelection(const QModelIndex &index)
{
TreeItem* item = static_cast<TreeItem*>(index.internalPointer());
QString filePath = "/path/to/" + item->data(0).toString() + ".html"; // Adjust the file path as per your file naming convention
qDebug() << item->data(0).toString();
// Load and display the HTML file in the QPlainTextEdit
QFile file(filePath);
if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
QTextStream stream(&file);
QString fileContent = stream.readAll();
textEdit->setHtml(fileContent);
file.close();
}
}
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QListWidget>
#include <QPlainTextEdit>
#include <QTreeView>
#include "custommodel.h"
#include <QTextBrowser>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void handleItemSelection(const QModelIndex &index); // Slot to handle item selection
private:
Ui::MainWindow *ui;
private:
QTreeView *treeView;
CustomModel *model;
QTextBrowser *textEdit;
QDockWidget *dockWidget;
};
#endif // MAINWINDOW_H
// TreeItem.h
#include <QString>
#include <QVariant>
class TreeItem
{
public:
TreeItem(const QString& name, TreeItem* parent = nullptr)
: m_name(name), m_parent(parent) {}
void addChild(TreeItem* child)
{
m_children.append(child);
}
TreeItem* child(int index) const
{
if (index >= 0 && index < m_children.size())
return m_children.at(index);
return nullptr;
}
int childCount() const
{
return m_children.size();
}
int columnCount() const
{
return 1; // Assuming only one column for the name
}
QVariant data(int column) const
{
if (column == 0)
return m_name;
return QVariant();
}
TreeItem* parent() const
{
return m_parent;
}
int row() const
{
if (m_parent)
return m_parent->m_children.indexOf(const_cast<TreeItem*>(this));
return 0;
}
private:
QString m_name;
TreeItem* m_parent;
QList<TreeItem*> m_children;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment