What works so far: opening a textfile via QFileDialog and showing the contents of the textfile in a Qlabel(showfile).
I too have a for-loop that counts how many \n are in the textfile.
Now what I want is that line after line in the textfile is assigned to a new Qlabel meaning each Qlabel contains one line of the textfile and place it dynamically on runtime.
Maybe you can help since I´m a little stuck here.
Here´s my qwidget class where the labels should be placed:
class mywidget:public QWidget //class for displaying everything
{
Q_OBJECT
private:
QGridLayout *area;
QLabel *showfile; //shows input from textfile
QLabel *processname,*status; //captionlabel
QFont *pfont,*sfont; //fontoption for processname&status
QLabel **processes; //2D array to dynamically create QLabels for each entry in file
public:
mywidget(QWidget *parent = Q_NULLPTR, Qt::WindowFlags flags = 0):QWidget(parent,flags)
{
this->area = new QGridLayout(this);
this->showfile = new QLabel(tr("Test"),this);
this->pfont = new QFont();
this->pfont->setPixelSize(20);
this->sfont = new QFont();
this->sfont->setPixelSize(20);
this->processname = new QLabel(tr("Process_Name:"),this);
this->processname->setFont(*pfont);
this->status = new QLabel(tr("Status:"),this);
this->status->setFont(*sfont);
this->area->addWidget(this->processname,0,0,Qt::AlignHCenter);
this->area->addWidget(this->status,0,1,Qt::AlignHCenter);
this->area->addWidget(this->showfile,1,0,Qt::AlignHCenter);
this->area->setSpacing(10);
}
~mywidget()
{
delete this->area;
delete this->showfile;
delete this->pfont;
delete this->sfont;
delete this->processname;
delete this->status;
}
friend class mywindow;
};
And here is my open-method from QMainwindow class:
void mywindow::oeffnen()
{
this->openfilename = QFileDialog::getOpenFileName //open textfile dialog
(this,
tr("Datei öffnen"),
QDir::homePath(),
"Textdateien (*.txt *.docx *.doc);;" "Alle Dateien (*.*)"
);
if(!this->openfilename.isEmpty())
{
this->file = new QFile(this->openfilename);
this->file->open(QIODevice::ReadOnly);
this->stream = new QTextStream(this->file);
this->fileread = this->stream->readAll();
for(int z = 0;z<this->fileread.length();++z) //check entries in string by counting \n
{
this->eintraege = this->fileread.count(QRegExp("\n"));
}
//this->s_eintraege = QString::number(this->eintraege); //converting to string for displaying
this->central->showfile->setText(this->fileread); //assign filecontent to label
if(!this->file->isReadable())
{
QMessageBox::information(this,
tr("Fehler"),
tr("Konnte Datei %1 nicht laden!").arg(this->openfilename)
);
}
else
{
QMessageBox::information(this,
tr("OK"),
tr("Konnte Datei %1 laden!").arg(this->openfilename)
);
}
this->file->close();
}
}
You can add a new QLabel into a layout for each new line you read from a file. You can store the labels in a container like QVector so you can access their text later on. Here is an example:
#include <QApplication>
#include <QWidget>
#include <QLabel>
#include <QFile>
#include <QLayout>
#include <QTextStream>
#include <QDebug>
class DisplayWidget : public QWidget
{
Q_OBJECT
public:
DisplayWidget(QWidget *parent = 0) : QWidget(parent)
{
labelLayout = new QVBoxLayout;
setLayout(labelLayout);
resize(200, 200);
}
void addLabel(const QString &text)
{
QLabel *label = new QLabel(text);
label_vector.append(label);
labelLayout->addWidget(label);
}
void readFile(const QString &filename)
{
QFile file(filename);
if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
return;
QTextStream ts(&file);
while(!ts.atEnd())
{
QString line = ts.readLine();
if(!line.isEmpty())
addLabel(line);
}
}
QString getLabelText(int index)
{
if(label_vector.size() > index)
return label_vector[index]->text();
return QString();
}
private:
QVBoxLayout *labelLayout;
QVector<QLabel*> label_vector;
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
DisplayWidget w;
w.readFile("somefile.txt");
w.show();
qDebug() << w.getLabelText(3);
return a.exec();
}
#include "main.moc"
Related
Problem: How to visualize a .txt or .csv file on a QTableView after triggering currentRowChanged(int currentRow) on QListWidget?
I have a very simple interface that I designed as a minimal verifiable example that look like below with a QPushButton a QLineEdit a QListWidget and a QTableView.
If I upload a .csv file using the QPushButton there are no problem and the content of the file is correctly shown on a QTableView:
However, and here is the problem:
When I drag and drop (functionality correctly implemented) multiple files and I click on one of them I have problem visualizing the file because the content does not change. It seems that the slot currentRowChanged(int currentRow) in the QListWidget is never triggered despite the files exists.
Below is the desired result:
Here is the example I implemented. If you copy/paste on your computer it will work no problem:
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QListWidgetItem>
#include <QStandardItemModel>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
enum fileFormat {
CSV,
TXT,
OTHER
};
MainWindow(QWidget *parent = nullptr);
~MainWindow();
void showFilesDetails(QListWidgetItem *item);
void readFile(QString path, fileFormat format);
void setValueAt(int ix, int jx, const QString &value);
protected:
void dragEnterEvent(QDragEnterEvent *event);
void dragLeaveEvent(QDragLeaveEvent *event);
void dragMoveEvent(QDragMoveEvent *event);
void dropEvent(QDropEvent *event);
private slots:
void on_listWidget_currentRowChanged(int currentRow);
void on_loadBtn_clicked();
private:
Ui::MainWindow *ui;
fileFormat *format;
QStandardItemModel *model;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDragEnterEvent>
#include <QDragLeaveEvent>
#include <QDragMoveEvent>
#include <QDropEvent>
#include <QMimeData>
#include <QDebug>
#include <QFileDialog>
namespace constants
{
const QStringList HEADERS = {
"tax_id", "Org_name", "GeneID", "CurrentID", "Status"};
}
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
setAcceptDrops(true);
model = new QStandardItemModel(this);
ui->tableView->setModel(model);
model->setHorizontalHeaderLabels(constants::HEADERS);
model->setColumnCount(constants::HEADERS.length());
ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeMode::ResizeToContents);
QHeaderView *horizontal = new QHeaderView(Qt::Horizontal, ui->tableView);
horizontal->setSectionsClickable(true);
horizontal->setHighlightSections(true);
ui->tableView->setHorizontalHeader(horizontal);
}
MainWindow::~MainWindow(){ delete ui; }
void MainWindow::dragEnterEvent(QDragEnterEvent *event) { event->accept(); }
void MainWindow::dragLeaveEvent(QDragLeaveEvent *event) { event->accept(); }
void MainWindow::dragMoveEvent(QDragMoveEvent *event) { event->accept(); }
void MainWindow::dropEvent(QDropEvent *event)
{
QString name;
QList<QUrl> urls;
QList<QUrl>::Iterator i;
urls = event->mimeData()->urls();
for(i = urls.begin(); i != urls.end(); ++i) {
name = i->path();
ui->listWidget->addItem(name);
}
}
void MainWindow::on_listWidget_currentRowChanged(int currentRow)
{
QListWidgetItem *item = nullptr;
if(ui->listWidget->count() > 0)
ui->listWidget->setCurrentRow(currentRow);
showFilesDetails(item);
}
void MainWindow::readFile(QString path, fileFormat format)
{
QListWidgetItem *item = nullptr;
QFile file(path);
if(!file.exists()){
qDebug() << "File does not exist "<<path;
}else{
qDebug() << path<< "File exist...";
}
if (file.fileName().endsWith(".txt", Qt::CaseInsensitive))
{
qDebug() << "File does not exist "<<file.fileName();
} else if(file.fileName().endsWith(".csv", Qt::CaseInsensitive))
{
qDebug() << "File does not exist "<<file.fileName();
}
QString line;
if (file.open(QIODevice::ReadOnly | QIODevice::Text)){
QTextStream stream(&file);
while (!stream.atEnd()){
line = stream.readLine();
ui->listWidget->setCurrentItem(item);
qDebug() << "line: "<<line;
}
}
file.close();
}
void MainWindow::showFilesDetails(QListWidgetItem *item)
{
QString path;
fileFormat frmt;
readFile(path, frmt);
ui->listWidget->setCurrentItem(item);
}
void MainWindow::on_loadBtn_clicked()
{
auto filename = QFileDialog::getOpenFileName(this, "Open", QDir::rootPath(), "CSV file (*.csv)");
if(filename.isEmpty()) {
ui->statusbar->showMessage("File empty");
return;
}
QFile file(filename);
if(!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
ui->statusbar->showMessage("File did not open");
return;
}
QTextStream xin(&file);
int ix = 0;
while (!xin.atEnd()) {
model->setRowCount(ix);
auto line = xin.readLine();
auto values = line.split(",");
const int colCount = values.size();
model->setColumnCount(colCount);
for(int jx = 0; jx < colCount; ++jx) {
setValueAt(ix, jx, values.at(jx));
}
++ix;
ui->lineEdit->setText(filename);
ui->statusbar->showMessage(filename);
}
ui->listWidget->addItem(filename);
file.close();
}
void MainWindow::setValueAt(int ix, int jx, const QString &value)
{
if (!model->item(ix, jx)) {
model->setItem(ix, jx, new QStandardItem(value));
} else {
model->item(ix, jx)->setText(value);
}
}
In order to solve the problem I read very useful sources and in particular I came across this one, this one and also I found this post useful. However, I am trying to figure out where the problem might be.
The drag/drop functionality is properly implemented but how do I upload the files in the QTableView and properly trigger the QListWidget event?
Thank to anyone who would have time to read and point to a potential solution.
afaik,funtion on_listWidget_currentRowChanged triggered properly.
function showFilesDetails passes an empty string to readFile,this may be the problem.
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDragEnterEvent>
#include <QDragLeaveEvent>
#include <QDragMoveEvent>
#include <QDropEvent>
#include <QMimeData>
#include <QDebug>
#include <QFileDialog>
namespace constants
{
const QStringList HEADERS = {
"tax_id", "Org_name", "GeneID", "CurrentID", "Status"};
}
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
setAcceptDrops(true);
model = new QStandardItemModel(this);
ui->tableView->setModel(model);
model->setHorizontalHeaderLabels(constants::HEADERS);
model->setColumnCount(constants::HEADERS.length());
ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeMode::ResizeToContents);
QHeaderView *horizontal = new QHeaderView(Qt::Horizontal, ui->tableView);
horizontal->setSectionsClickable(true);
horizontal->setHighlightSections(true);
ui->tableView->setHorizontalHeader(horizontal);
}
MainWindow::~MainWindow(){ delete ui; }
void MainWindow::dragEnterEvent(QDragEnterEvent *event) { event->accept(); }
void MainWindow::dragLeaveEvent(QDragLeaveEvent *event) { event->accept(); }
void MainWindow::dragMoveEvent(QDragMoveEvent *event) { event->accept(); }
void MainWindow::dropEvent(QDropEvent *event)
{
QString name;
QList<QUrl> urls;
QList<QUrl>::Iterator i;
urls = event->mimeData()->urls();
for(i = urls.begin(); i != urls.end(); ++i) {
name = i->toLocalFile();
ui->listWidget->addItem(name);
}
}
void MainWindow::on_listWidget_currentRowChanged(int currentRow)
{
QListWidgetItem *item = nullptr;
if(ui->listWidget->count() > 0)
ui->listWidget->setCurrentRow(currentRow);
showFilesDetails(item);
}
void MainWindow::readFile(QString path, fileFormat format)
{
QListWidgetItem *item = nullptr;
QFile file(path);
if(!file.exists()){
qDebug() << "File does not exist "<<path;
}else{
qDebug() << path<< "File exist...";
}
if (file.fileName().endsWith(".txt", Qt::CaseInsensitive))
{
qDebug() << "File does not exist "<<file.fileName();
} else if(file.fileName().endsWith(".csv", Qt::CaseInsensitive))
{
qDebug() << "File does not exist "<<file.fileName();
}file.open(QIODevice::ReadOnly | QIODevice::Text);
QTextStream xin(&file);
int ix = 0;
while (!xin.atEnd()) {
model->setRowCount(ix);
auto line = xin.readLine();
auto values = line.split(",");
const int colCount = values.size();
model->setColumnCount(colCount);
for(int jx = 0; jx < colCount; ++jx) {
setValueAt(ix, jx, values.at(jx));
}
++ix;
ui->lineEdit->setText(path);
ui->statusbar->showMessage(path);
}
file.close();
}
void MainWindow::showFilesDetails(QListWidgetItem *item)
{
item=ui->listWidget->currentItem();
QString path=
item->data(Qt::DisplayRole).toString();;
fileFormat frmt;
readFile(path, frmt);
ui->listWidget->setCurrentItem(item);
}
void MainWindow::on_loadBtn_clicked()
{
auto filename = QFileDialog::getOpenFileName(this, "Open", QDir::rootPath(), "CSV file (*.csv)");
if(filename.isEmpty()) {
ui->statusbar->showMessage("File empty");
return;
}
QFile file(filename);
if(!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
ui->statusbar->showMessage("File did not open");
return;
}
QTextStream xin(&file);
int ix = 0;
while (!xin.atEnd()) {
model->setRowCount(ix);
auto line = xin.readLine();
auto values = line.split(",");
const int colCount = values.size();
model->setColumnCount(colCount);
for(int jx = 0; jx < colCount; ++jx) {
setValueAt(ix, jx, values.at(jx));
}
++ix;
ui->lineEdit->setText(filename);
ui->statusbar->showMessage(filename);
}
ui->listWidget->addItem(filename);
file.close();
}
void MainWindow::setValueAt(int ix, int jx, const QString &value)
{
if (!model->item(ix, jx)) {
model->setItem(ix, jx, new QStandardItem(value));
} else {
model->item(ix, jx)->setText(value);
}
}
In an earlier Question I asked about why my program/QTcpServer was crashing all the time, and I really appreciate the help I got from everyone.
However, I have decided to try and rebuild the Program from scratch in order to see why it crashes, and I may have found something.
Allow me to post the new Code below:
//mainwindow.h
#include <QMainWindow>
#include <QPushButton>
#include <QTextEdit>
#include <QStandardItemModel>
#include <QTcpServer>
#include <QTcpSocket>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
void startServer();
void handleConnection();
void readMessage();
void ReadXML(QString XMLString);
private:
QPushButton *SServer;
QTextEdit * ContainerView; //not changing to QTableView just yet
int Pallet_Number;
QTcpServer *tcpServer;
QTcpSocket *tcpSocket;
};
//mainwindow.cpp
#include "mainwindow.h"
#include <QLabel>
#include <QLayout>
#include <QDebug>
#include <QDomDocument>
#include <QRegularExpression>
static QRegularExpression Box_Code("B");
static QRegularExpression Cylinder_Code("C");
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent),
SServer(new QPushButton("Start Listening")),
ContainerView(new /*QTableView*/QTextEdit),
tcpServer(new QTcpServer),
tcpSocket(new QTcpSocket)
{
QStringList HeaderRow;
HeaderRow.append("Pallet");
HeaderRow.append("Container");
HeaderRow.append("Code");
HeaderRow.append("Height");
HeaderRow.append("Breadth/Diameter");
HeaderRow.append("Length");
HeaderRow.append("Weight");
resize(800,300);
connect(SServer, &QPushButton::clicked, this, &MainWindow::startServer);
connect(tcpServer, &QTcpServer::newConnection, this, &MainWindow::handleConnection);
connect(tcpSocket, &QTcpSocket::readyRead, this, &MainWindow::handleConnection);
QLabel *Lab1(new QLabel("Listening on Port 6164"));
QHBoxLayout *HB(new QHBoxLayout);
QVBoxLayout *VB(new QVBoxLayout);
HB->addWidget(SServer);
HB->addWidget(Lab1);
VB->addItem(HB);
VB->addWidget(ContainerView);
QWidget *window(new QWidget);
window->setLayout(VB);
setCentralWidget(window);
}
MainWindow::~MainWindow()
{
}
void MainWindow::startServer()
{
if(!tcpServer->listen(QHostAddress::LocalHost, 6164)){
qDebug() << "Error connecting to Server";
tcpServer->close();
return;
} else {
qDebug() << "Started Successfully";
}
}
void MainWindow::handleConnection()
{
tcpSocket = tcpServer->nextPendingConnection();
qDebug() << "New Connection!";
connect(tcpSocket, &QTcpSocket::readyRead, this, &MainWindow::readMessage);
}
void MainWindow::readMessage()
{
ContainerView->clear(); //clear table to prepare for new XMLString
QByteArray buffer = tcpSocket->readAll();
QString FromContainer = QString::fromUtf8(buffer);
ReadXML(FromContainer);
}
void MainWindow::ReadXML(QString XMLString)
{
ContainerView->append(XMLString);
QDomDocument Xml_String;
Xml_String.setContent(XMLString);
QDomElement Xml_Root = Xml_String.documentElement();
if(Xml_Root.tagName() == "Pallets")
{
QDomElement Xml_Pallet = Xml_Root.firstChildElement();
while(!Xml_Pallet.isNull())
{
if(Xml_Pallet.tagName() == "Pallet")
{
int PN = Xml_Pallet.attribute("Number").toInt(nullptr,10);
QDomElement Box_Cyl = Xml_Pallet.firstChildElement();
while(!Box_Cyl.isNull())
{
if(Box_Cyl.tagName() == "Box")
{
QString BC = Box_Cyl.tagName();
ContainerView->append("Pallet No. " + QString::number(PN) + "\nContainer: " + BC);
// QDomElement Box_Info = Box_Cyl.firstChildElement();
// while(!Box_Info.isNull())
// {
// ...more code
// Box_Info = Box_Info.nextSiblingElement();
// }
} else if(Box_Cyl.tagName() == "Cylinder")
{
QString BC = Box_Cyl.tagName();
ContainerView->append("Pallet No. " + QString::number(PN) + "\nContainer: " + BC);
}
Box_Cyl = Box_Cyl.nextSiblingElement();
}
}
Xml_Pallet = Xml_Pallet.nextSiblingElement();
}
}
}
The XML String I am trying to read is shown below:
When I include the commented code to read the Elements within 'Box' or 'Cylinder' (I altered the code to check both sides), the program freezes.
This hasn't happened to me before, so I have no idea what to do.
I'm trying to set up a QFormLayout so that some buttons show or hide the part below them, and using setHidden on the widgets I want to hide is resulting in a bad layout due to the form padding still showing.
So I tried this:
#include <QApplication>
#include <QCheckBox>
#include <QLabel>
#include <QFormLayout>
#include <QMainWindow>
class MainWindow : public QMainWindow
{
Q_OBJECT
QCheckBox checkA;
QFormLayout * m_formLayout;
QWidget * m_widget;
public:
explicit MainWindow(QWidget *parent = 0) :
QMainWindow(parent),
m_formLayout(0L),
m_widget(0L)
{
populate();
connect(&checkA, &QCheckBox::stateChanged, this, &MainWindow::populate);
}
virtual ~MainWindow() { clear(); }
bool doesOwnObject(void * it) const
{
return (uintptr_t)this <= (uintptr_t)it && (uintptr_t)it < (uintptr_t)(this+1);
}
void clear()
{
if(m_formLayout)
{
QLayoutItem *child;
while ((child = m_formLayout->takeAt(0)) != 0)
{
QLayout * layout = child->layout();
QSpacerItem * spacer = child->spacerItem();
QWidget * widget = child->widget();
if(layout && !doesOwnObject(layout)) delete layout;
if(spacer && !doesOwnObject(spacer)) delete spacer;
if(widget && !doesOwnObject(widget)) delete widget;
}
delete m_formLayout;
m_formLayout = 0L;
}
}
void populate()
{
if(m_widget) { clear(); return; }
m_widget = new QWidget(this);
setCentralWidget(m_widget);
m_formLayout = new QFormLayout(m_widget);
m_formLayout->addRow(tr("Show Check Box B:"), &checkA);
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
But that doesn't seem to work as clear() doesn't seem to be actually depopulating the form layout. The widgets remain where they were before the QFormLayout was deleted. And if m_widget is deleted then the program will crash as it tries to delete checkA, because checkA has not been removed from the now deleted form.
How do I fix this?
I have come to a solution that has the desirable (or what I understood) behavior. However, I've moved everything to pointers (since is what I am used to with Qt).
Header file:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QCheckBox>
#include <QLayoutItem>
#include <QFormLayout>
#include <QMainWindow>
#include <QLabel>
class MainWindow : public QMainWindow
{
Q_OBJECT
QCheckBox *checkA, *checkB;
QLabel *labelA, *labelB, *labelC;
QFormLayout * m_formLayout;
public:
explicit MainWindow(QWidget *parent = 0);
virtual ~MainWindow();
void clear();
public slots:
void populate();
};
#endif // MAINWINDOW_H
cpp file:
#include "mainWindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{
QWidget * centralWidget = new QWidget(this);
setCentralWidget(centralWidget);
m_formLayout = new QFormLayout(centralWidget);
checkA = new QCheckBox;
labelA = new QLabel("Show Check Box B:");
connect(checkA, &QCheckBox::stateChanged, this, &MainWindow::populate);
int row = 0;
m_formLayout->insertRow(row++, labelA, checkA);
checkB = new QCheckBox;
labelB = new QLabel("Check Box B:");
m_formLayout->insertRow(row++, labelB, checkB);
labelC = new QLabel("Bottom label");
m_formLayout->insertRow(row++,labelC);
populate();
}
MainWindow::~MainWindow() {
}
void MainWindow::clear()
{
if (!checkA->isChecked())
{
checkB->hide();
labelB->hide();
}
}
void MainWindow::populate()
{
clear();
if(checkA->isChecked())
{
checkB->show();
labelB->show();
}
}
Solution:
void clear()
{
QLayoutItem *child;
while ((child = m_formLayout->takeAt(0)) != 0)
{
QLayout * layout = child->layout();
QSpacerItem * spacer = child->spacerItem();
QWidget * widget = child->widget();
if(layout && !doesOwnObject(layout)) delete layout;
if(spacer && !doesOwnObject(spacer)) delete spacer;
if(widget)
{
if(doesOwnObject(widget)) widget->setParent(0L);
else delete widget;
}
}
}
setParent had to be called.
I have created a ui with QT designer.
In that ui i use one pushbutton for browse directories,and one lineedit for select dir path text to show, and another pushbuton for ok, these 3 widgets and one treewidget are placed on a groupbox horizontally.
What I would want to know is if i browse and click ok button then i want to display the treeview to that path on the ui
my test1.h file contains
#ifndef LINEEDIT_H
#define LINEEDIT_H
#include <QtGui>
#include "ui_line1.h"
#include <stdlib.h>
#include <iostream>
using namespace Ui;
class line : public QMainWindow
{
Q_OBJECT
public:
line(QWidget *parent = 0);
QTreeWidget(parent);
void viewer(QTreeWidgetItem* item,QString filePath);
private:
Ui::lineedit ui;
QDirModel *model;
public slots:
void browse();
void treeview();
void showDirectory(QTreeWidgetItem* item, int /*column*/);
};
#endif // LINEEDIT_H
my test1.cpp file contains
#include "test1.h"
#include <QTreeWidget>
#include <QtGui>
#include <stdlib.h>
#include <iostream>
#include<QWidget>
#include<QTreeWidgetItem>
line::line(QWidget* parent)
{
ui.setupUi(this);
connect(ui.browse, SIGNAL(clicked()), this,SLOT(browse()));
connect(ui.ok, SIGNAL(clicked()), this,SLOT(treeview()));
connect(this , SIGNAL(itemClicked(QTreeWidgetItem*,int)),this,SLOT(showDirectory(QTreeWidgetItem*,int)));
}
void line::browse()
{
QString dir = QFileDialog::getExistingDirectory(this, tr("Open Directory"),
"/home",QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
ui.lineEdit->setText(dir);
}
void line::treeview()
{
QWidget *centralWidget = new QWidget();
line *tree = new line (centralWidget);
tree->setFixedSize(395,395);
QTreeWidgetItem* headerItem = new QTreeWidgetItem();
headerItem->setText(0,QString("File Name"));
headerItem->setText(1,QString("Size (Bytes)"));
headerItem->setText(2,QString("Path"));
ui.treeWidget->setHeaderItem(headerItem);
ui.treeWidget->setColumnCount(2);
//ui.treeView->
//setHeaderItem(headerItem);
QString strdir=ui.lineEdit->text();
QDir* rootDir = new QDir(strdir);
QFileInfoList filesList = rootDir->entryInfoList();
foreach(QFileInfo fileInfo, filesList)
{
QTreeWidgetItem* item = new QTreeWidgetItem();
item->setText(0,fileInfo.fileName());
if(fileInfo.isFile())
{
item->setText(1,QString::number(fileInfo.size()));
item->setIcon(0,*(new QIcon("file.jpg")));
}
if(fileInfo.isDir())
{
item->setIcon(0,*(new QIcon("folder.jpg")));
viewer(item,fileInfo.filePath());
}
item->setText(2,fileInfo.filePath());
ui.treeWidget->addTopLevelItem(item);
}
QPalette* palette = new QPalette();
palette->setColor(QPalette::Base,Qt::red);
ui.treeWidget->setPalette(*(palette));
/*window->setCentralWidget(centralWidget);*/
/*ui.treeWidget->addTopLevelItem(centralWidget);*/
/*window->show();*/
return treeview();
}
void line::viewer(QTreeWidgetItem* item,QString filePath)
{
QDir* rootDir = new QDir(filePath);
QFileInfoList filesList = rootDir->entryInfoList();
foreach(QFileInfo fileInfo, filesList)
{
QTreeWidgetItem* child = new QTreeWidgetItem();
child->setText(0,fileInfo.fileName());
if(fileInfo.isFile())
{
child->setText(1,QString::number(fileInfo.size()));
}
if(fileInfo.isDir())
{
child->setIcon(0,*(new QIcon("folder.jpg")));
child->setText(2,fileInfo.filePath());
}
item->addChild(child);
}
}
void line::showDirectory(QTreeWidgetItem* item, int )
{
QDir* rootDir = new QDir(item->text(2));
QFileInfoList filesList = rootDir->entryInfoList();
foreach(QFileInfo fileInfo, filesList)
{
QTreeWidgetItem* child = new QTreeWidgetItem();
child->setText(0,fileInfo.fileName());
if(fileInfo.isFile())
{
child->setText(1,QString::number(fileInfo.size()));
child->setIcon(0,*(new QIcon("file.jpg")));
}
if(fileInfo.isDir())
{
child->setIcon(0,*(new QIcon("folder.jpg")));
child->setText(2,fileInfo.filePath());
}
item->addChild(child);
/*resizeColumnToContents(0);*/
}
}
main.cpp contains
#include "test1.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
line w;
w.show();
return a.exec();
}
am not getting output, while am checking on the debug points each and every thing fine but not getting the output on the ui treewidget
Is this possible, please help me where can i modify my code.
Thanks all.
test1.cppfile:
#include "test1.h"
#include <QTreeWidget>
#include <QtGui>
#include <stdlib.h>
#include <iostream>
#include<QWidget>
#include<QTreeWidgetItem>
line::line(QWidget* parent)
{
ui.setupUi(this);
connect(ui.browse, SIGNAL(clicked()), this,SLOT(browse()));
connect(ui.ok, SIGNAL(clicked()), this,SLOT(treeview()));
connect(this , SIGNAL(itemClicked(QTreeWidgetItem*,int)),this,SLOT(showDirectory(QTreeWidgetItem*,int)));
}
void line::browse()
{
QString dir = QFileDialog::getExistingDirectory(this, tr("Open Directory"),
"/home",QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
ui.lineEdit->setText(dir);
}
void line::treeview()
{
QString strdir=ui.lineEdit->text();
QDir* rootDir = new QDir(strdir);
QFileInfoList filesList = rootDir->entryInfoList();
/*QTreeWidgetItem* item = new QTreeWidgetItem();*/
ui.treeWidget->setColumnCount(1);
QList<QTreeWidgetItem *> items;
foreach(QFileInfo fileInfo, filesList)
{
QTreeWidgetItem* item = new QTreeWidgetItem();
item->setText(0,fileInfo.fileName());
if(fileInfo.isFile())
{
item->setText(1,QString::number(fileInfo.size()));
item->setIcon(0,*(new QIcon("file.jpg")));
/*treeItem->setCurrentItem(item);*/
}
if(fileInfo.isDir())
{
item->setIcon(0,*(new QIcon("folder.jpg")));
viewer(item,fileInfo.filePath());
}
item->setText(2,fileInfo.filePath());
ui.treeWidget->addTopLevelItem(item);
/* ui.treeWidget->insertTopLevelItems(0, item);*/
items.append(new QTreeWidgetItem((QTreeWidget*)0, QStringList(QString("item: %1"))));
ui.treeWidget->insertTopLevelItems(0, items);
}
}
void line::viewer(QTreeWidgetItem* item,QString filePath)
{
QDir* rootDir = new QDir(filePath);
QFileInfoList filesList = rootDir->entryInfoList();
foreach(QFileInfo fileInfo, filesList)
{
QTreeWidgetItem* child = new QTreeWidgetItem();
child->setText(0,fileInfo.fileName());
if(fileInfo.isFile())
{
child->setText(1,QString::number(fileInfo.size()));
}
if(fileInfo.isDir())
{
child->setIcon(0,*(new QIcon("folder.jpg")));
child->setText(2,fileInfo.filePath());
}
item->addChild(child);
ui.treeWidget->addTopLevelItem(item);
}
}
void line::showDirectory(QTreeWidgetItem* item, int )
{
QDir* rootDir = new QDir(item->text(2));
QFileInfoList filesList = rootDir->entryInfoList();
foreach(QFileInfo fileInfo, filesList)
{
QTreeWidgetItem* child = new QTreeWidgetItem();
child->setText(0,fileInfo.fileName());
if(fileInfo.isFile())
{
child->setText(1,QString::number(fileInfo.size()));
child->setIcon(0,*(new QIcon("file.jpg")));
}
if(fileInfo.isDir())
{
child->setIcon(0,*(new QIcon("folder.jpg")));
child->setText(2,fileInfo.filePath());
}
item->addChild(child);
ui.treeWidget->addTopLevelItem(item);
/*resizeColumnToContents(0);*/
}
}
test1.h file:
#ifndef LINEEDIT_H
#define LINEEDIT_H
#include <QtGui>
#include "ui_line1.h"
#include <stdlib.h>
#include <iostream>
using namespace Ui;
class line : public QMainWindow
{
Q_OBJECT
public:
line(QWidget *parent = 0);
QTreeWidget(parent);
void viewer(QTreeWidgetItem* item,QString filePath);
private:
Ui::lineedit ui;
QDirModel *model;
public slots:
void browse();
void treeview();
void showDirectory(QTreeWidgetItem* item, int /*column*/);
};
#endif // LINEEDIT_H
main.cpp file:
#include "test1.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
line w;
w.show();
return a.exec();
}
I am creating a set of QCheckBox dynamically based on some user input like so:
QWidget *wid = new QWidget();
QVBoxLayout *layout = new QVBoxLayout();
for(int i=0; i<NumberModes; i++)
{
int k = Amplitudes(i,0);
int m = Amplitudes(i,1);
QString ks = QString::number(k);
QString ms = QString::number(m);
QString position = QString::number(i);
QString mode = "A"+ks+ms;
QCheckBox *check = new QCheckBox(mode);
connect(check, SIGNAL(toggled(bool)), &mapper, SLOT(map()));
connect(check, SIGNAL(toggled(bool)), &SelectModes, SLOT(map()));
mapper.setMapping(check,position);
SelectModes.setMapping(check,mode);
layout->addWidget(check);
updateGeometry();
}
wid->setLayout(layout);
ui->scrollArea->setWidget(wid);
The QSignalMapper are then connected to another class that performs some calculations:
connect(&SelectModes, SIGNAL(mapped(QString)), this, SIGNAL(CheckBoxClicked2(QString)));
connect(this, SIGNAL(CheckBoxClicked2(QString)), &Supress2, SLOT(ListenSelectedModes(QString)));
connect(&mapper, SIGNAL(mapped(QString)), this, SIGNAL(CheckBoxClicked(QString)));
connect(this, SIGNAL(CheckBoxClicked(QString)), &Suppress, SLOT(ListenSelectedModes(QString)));
What I need is that the classes only receive signals when the QCheckBox are checked; meaning if you check it once, and then un-check it no signal should be emitted, or received. Not sure what the best approach is.
Any ideas?
With C++11 it's doable simply and without QSignalMapper. Here's an working example.
#include <QWidget>
#include <QCheckBox>
#include <QVBoxLayout>
class QCheckBox;
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
signals:
void checkBoxChecked(QCheckBox *checkBox);
};
Widget::Widget(QWidget *parent) :
QWidget(parent)
{
QVBoxLayout *layout = new QVBoxLayout;
for (int i = 0; i < 10; ++i) {
QCheckBox *checkBox = new QCheckBox("CheckBox " + QString::number(i + 1));
connect(checkBox, &QCheckBox::toggled, [=](bool checked) {
if (checked)
emit checkBoxChecked(checkBox);
});
layout->addWidget(checkBox);
}
setLayout(layout);
}
The suggestions given by user2672165 are excellent!
If you want to monitor only the check event but not the uncheck event, one way would be to subclass the QCheckBox widget so that it emits a particular signal only when the checkbox is checked (e.g. checkBoxChecked)
Then you connect your signal mapper to the custom signal checkBoxChecked, instead of the standard toggle(bool) signal.
In this way the slot associated to the signal mapper is invoked only when the checkbox is checked and not when it is unchecked.
Here is a simple example
#include <QApplication>
#include <QtGui>
#include <QVBoxLayout>
#include <QSignalMapper>
#include <QCheckBox>
#include <QDebug>
class CheckableCheckBox : public QCheckBox {
Q_OBJECT
public:
CheckableCheckBox(const QString &text, QWidget *parent = 0)
: QCheckBox(text, parent)
{
connect(this, SIGNAL(toggled(bool)),
this, SLOT(verifyCheck(bool)));
}
signals:
void checkBoxChecked();
public slots:
void verifyCheck(bool checked) {
if (checked)
emit checkBoxChecked();
}
};
class Test : public QWidget {
Q_OBJECT
public:
Test(QWidget *parent = 0) : QWidget(parent) {
QSignalMapper *mapper = new QSignalMapper();
QVBoxLayout *layout = new QVBoxLayout();
for (int i = 0; i < 10; i++) {
QString mode = "A" + QString::number(i);
CheckableCheckBox *check = new CheckableCheckBox(mode);
connect(check, SIGNAL(checkBoxChecked()),
mapper, SLOT(map()));
mapper->setMapping(check, QString::number(i));
layout->addWidget(check);
setLayout(layout);
}
connect(mapper, SIGNAL(mapped(QString)),
this, SLOT(CheckBoxClicked(QString)));
}
public slots:
void CheckBoxClicked(const QString &mapping) {
qWarning() << "Checkbox:" << mapping << " is checked";
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Test *wid = new Test();
wid->show();
return a.exec();
}
#include "main.moc"
Edit:
If you want to monitor a change in the check status and then notify to some other portions of the code the status of the checkbox (which is probably what you want) you can do something like this... You don't even need a QSignalMapper...
I have implemented a test method testMonitorCheckStatus to show what I mean. Note that you need typedef QList<bool> CheckBoxStatusList; (at least as far as I know) to use QList as an argument to slots and signals.
Edit #2:
The number of checkboxes is set at object creation
Hope this helps
#include <QApplication>
#include <QtGui>
#include <QVBoxLayout>
#include <QSignalMapper>
#include <QCheckBox>
#include <QList>
#include <QDebug>
typedef QList<bool> CheckBoxStatusList;
class Test : public QWidget {
Q_OBJECT
public:
Test(int totalCheckboxes, QWidget *parent = 0) : QWidget(parent)
{
QVBoxLayout *layout = new QVBoxLayout();
for (int i = 0; i < totalCheckboxes; i++) {
QString mode = "A" + QString::number(i);
QCheckBox *checkBox = new QCheckBox(mode);
connect(checkBox, SIGNAL(toggled(bool)),
this, SLOT(monitorCheckStatus()));
m_checkBoxList.append(checkBox);
layout->addWidget(checkBox);
}
setLayout(layout);
connect(this, SIGNAL(checkBoxStatusChanged(CheckBoxStatusList)),
this, SLOT(testMonitorCheckStatus(CheckBoxStatusList)));
}
public slots:
void monitorCheckStatus() {
CheckBoxStatusList checkBoxStatus;
for (int i = 0; i < m_checkBoxList.count(); ++i)
checkBoxStatus.append(m_checkBoxList.at(i)->isChecked());
emit checkBoxStatusChanged(checkBoxStatus);
}
void testMonitorCheckStatus(const CheckBoxStatusList &checkBoxStatus) {
for (int i = 0; i < checkBoxStatus.count(); ++i)
qWarning() << "Checkbox:" << i << " is" << (checkBoxStatus.at(i) ? "checked" : "unchecked");
qWarning(" ");
}
signals:
void checkBoxStatusChanged(const CheckBoxStatusList &checkBoxStatus);
private:
QList<QCheckBox *> m_checkBoxList;
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Test *wid = new Test(10);
wid->show();
return a.exec();
}
#include "main.moc"