How to update QWindow in qt c++, text doesn't show - c++

I'm writing QT deskop app and I have a small issue.
I want to open new tab with text that I put from a file.
I have two class one in mainwindow.h and second in form.h
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
public slots:
void on_open_file_clicked();
void on_search_keyword_clicked();
void on_search_tag_clicked();
void on_tabWidget_tabCloseRequested(int index);
void show_tab(QString keywords);
QString return_text();
public:
Ui::MainWindow *ui;
private slots:
void on_actionOpen_file_triggered();
void on_actionShow_text_file_triggered();
QVector<QString>find_logs_keywords(QString keywords);
private:
QString text = "example text";
};
#endif // MAINWINDOW_H
form.h
#ifndef FORM_H
#define FORM_H
#include <QWidget>
namespace Ui {
class Form;
}
class Form : public QWidget
{
Q_OBJECT
public:
explicit Form(QWidget *parent = nullptr);
~Form();
public slots:
void on_pushButton_2_clicked();
void text_to_plain(QString& text);
private:
Ui::Form *ui;
};
#endif // FORM_H
mainwindow.cpp
#include <QFileDialog>
#include <QFile>
#include <QTextStream>
#include <QMessageBox>
#include <QDir>
#include <QVector>
#include <QStringList>
#include <iostream>
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "search_keyword.h"
#include "search_tag.h"
#include "form.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_open_file_clicked()
{
QString filter = "log file (*log) ;; Tex File (*.txt)";
QString file_name = QFileDialog::getOpenFileName(this, "Open a file", QDir::homePath(), filter);
QMessageBox::information(this,"..",file_name);
QFile file(file_name);
if (!file.open(QFile::ReadOnly | QFile::Text)) QMessageBox::warning(this,"title","file not open");
else
{
QTextStream in(&file);
text = in.readAll();
ui->plainTextEdit->setPlainText(text);
ui->lineEdit->setPlaceholderText(file_name);
}
file.close();
}
void MainWindow::on_actionShow_text_file_triggered()
{
show_tab("all logs");
Form Fr;
Fr.text_to_plain(text);
}
void MainWindow::show_tab(QString keywords)
{
ui->tabWidget->addTab(new Form(), QString("%1").arg(keywords));
ui->tabWidget->setCurrentIndex(ui->tabWidget->count()-1);
}
form.cpp
#include "form.h"
#include "ui_form.h"
Form::Form(QWidget *parent) :
QWidget(parent),
ui(new Ui::Form)
{
ui->setupUi(this);
}
Form::~Form()
{
delete ui;
}
void Form::text_to_plain(QString& text)
{
ui->plainTextEdit->setPlainText(text);
}
My problem is after call method void MainWindow::on_actionShow_text_file_triggered() plain text in QWidget in Form is still empty i tried: this->update, this->repaint, Form::update(); Form::repaint(), QWidget::repaint(); QWidget::update();. Everything fail.

the problem is that your Form exists only in this function scope.
void MainWindow::on_actionShow_text_file_triggered()
{
show_tab("all logs");
Form Fr;
Fr.text_to_plain(text);
}
Here -> Form Fr; the Form is created: constructor(explicit Form(QWidget *parent = nullptr);) of the class Form is called
And at this place -> } your Form is deleted: destructor(~Form();) of class Form is called.
This happens because you create your Form instance on the function stack. So Fr is local object and exist only in the scope of the function on_actionShow_text_file_triggered() after the function is executed/finished all local variables are freed/destructed.
if your Fr object should exist not just in this function you need to create it on the heap instead of stack.
P.S.: for case study read about stack vs heap for example hear: What and where are the stack and heap?
Your on_actionShow_text_file_triggered function should then look like this:
void MainWindow::on_actionShow_text_file_triggered()
{
show_tab("all logs");
Form * Fr = new Form(this); //if you set parent object your Form will be automatically deleted/destructed then MainWindow is deleted/destructed
Fr->text_to_plain(text);
Fr->show(); // you should call show() function of QWidget class and subclasses to get your widget visible
}

This Form * Fr = new Form(this); will create new form pointer every time. Would not it be better to create one global pointer to the form, call the functions and delete it later?

Related

How is possible put QGraphicsView to own method as argument?

So, I have the source code for a training. Where I want to do operation with QGraphicsView named pp in the own class ProcessPicture.
I would ask you, is it possible to put the QGraphicsView object to the method of own class? If yes, please tell me how or lead me on answer.
For a detailed description after releasing button will done the on_processPic_pushButton_released() where is called the method of pp object. This method take a ui->graphicsview as argument. This argument will used in the pp.drawrectangle(ui->graphicsview).
The code is without error or warning, only it is not draw the rectangle to the ui->graphicsview. When I take a source code from pp.drawrectangle() and try it, in the on_processPic_pushButton_released() all is fine. So I think that i miss some knowledge.
Lets look on my source code.
processpicture.h
#ifndef PROCESSPICTURE_H
#define PROCESSPICTURE_H
#include "QString"
#include "QPicture"
#include "QGraphicsView"
class ProcessPicture
{
public:
ProcessPicture();
void draw(QGraphicsView out);
void draw_rectangle(QGraphicsView out);
private:
QString path = "***/Untitled.bmp";
QImage img;
};
#endif // PROCESSPICTURE_H
processpicture.cpp
#include "processpicture.h"
#include "QGraphicsScene"
#include "QGraphicsPixmapItem"
ProcessPicture::ProcessPicture()
{
img.load(path);
}
void ProcessPicture::draw_rectangle(QGraphicsView out){
//QGraphicsScene* scene = new QGraphicsScene();
QGraphicsScene scene;
QRect rect;
rect.setRect(10,10,10,10);
scene.addRect(rect);
out.setScene(&scene);
out.show();
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include "processpicture.h"
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
ProcessPicture *pp;
//pp = new ProcessPicture;
~MainWindow();
private:
private slots:
void on_processPic_pushButton_released();
private:
Ui::MainWindow *ui;
bool event(QEvent *event);
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
pp = new ProcessPicture;
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_processPic_pushButton_released()
{
//pp->draw(ui->graphicsView);
pp->draw_rectangle(ui->graphicsView);
}
For every non-toxic answer I thank.

Way to find clicked ListItem on ListWidget

I want to get ListItem in ListWidget that I clicked. But I can't find the way. All the things say get from text but I want the clicked one. Also I'm new in qt and c++.
stackoverflow says add more detail but I don't know how to add more detail so I write this line to get enough "detail".
Here is my .h file:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <string.h>
#include <QListWidgetItem>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
class rsu
{
public:
std::string id;
std::string name;
double coorx;
double coory;
std::string ip;
int port;
};
std::vector<rsu> r;
QListWidgetItem *rsuitem;
private:
Ui::MainWindow *ui;
private slots:
void addRSU();
void deleteRSU();
};
#endif // MAINWINDOW_H
And my .cpp file:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "string"
#include "string.h"
#include "QCheckBox"
#include "QString"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->rsuWidget->hide();
connect(ui->addRSU, SIGNAL(clicked()), ui->rsuWidget, SLOT(show()));
connect(ui->cancelRSU, SIGNAL(clicked()), ui->rsuWidget, SLOT(hide()));
connect(ui->saveRSU, SIGNAL(clicked()), this, SLOT(addRSU()));
}
MainWindow::~MainWindow()
{
delete ui;
delete rsuitem;
}
void MainWindow::addRSU()
{
rsu *baz = new rsu;
baz->id = ui->ID->text().toStdString();
baz->name = ui->Name->text().toStdString();
baz->coorx = ui->CoorX->text().toFloat();
baz->coorx = ui->CoorX->text().toFloat();
baz->ip = ui->IP->text().toStdString();
baz->port = ui->Port->text().toInt();
r.push_back(*baz);
rsuitem = new QListWidgetItem(QString::fromUtf8(baz->name.c_str()),ui->listRSU);
rsuitem->setFlags(rsuitem->flags() | Qt::ItemIsUserCheckable);
rsuitem->setCheckState(Qt::Unchecked);
}
void MainWindow::deleteRSU()
{
}
And my ui:

Trying to display instantly the result of a user editing action when clicking an image in Qt

I am trying to implement an image editor with some customized image edition tools not present in the Qt image classes. When the user clicks on the image scene to perform some operation, I want the image to be instantly updated in the GUI application, showing in real time to the user the changes (drawing pixels, zooming...). The problem is that the actions to edit the image when clicking can only be done (at least to my knowledge) inside a separate class (in the example I show below, such class is called GraphicsScene), and thus I don't know how to transfer the edited image to the MainWindow class.
In short, I would like to "send" the edited image from the GraphicsScene class to the MainWindow class immediately after the user performs an action to edit the image, and then make the latter one execute the code to update the screen in real time after each user action is performed on the image.
For the sake of clarity, I next show the scheme of the code I have for now.
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>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
Ui::MainWindow *ui;
private slots:
private:
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFileDialog>
#include <QApplication>
#include <QMouseEvent>
#include <QGraphicsSceneMouseEvent>
#include "graphicsscene.h"
extern QImage Image_original, Image_modified;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_actionOpen_Image_triggered()
{
QDir dir;
QString filename=QFileDialog::getOpenFileName(this,
tr("Open Background"),
path,
tr("Images (*.png *.bmp *.jpg *.jpeg);;All files (*.*)"));
Image_original.load(filename);
GraphicsScene * img = new GraphicsScene( this );
img->addPixmap(QPixmap::fromImage(Image_original));
ui->preview->setScene(img);
}
In order to be able to track the coordinates when clicking, and following some suggestions around the net, I created a subclass of QGraphicsScene called GraphicsScene, whose header file is:
graphicsscene.h
#ifndef GRAPHICSSCENE_H
#define GRAPHICSSCENE_H
#include <QGraphicsScene>
#include <QGraphicsSceneMouseEvent>
#include <QPointF>
#include <QList>
class GraphicsScene : public QGraphicsScene
{
Q_OBJECT
public:
explicit GraphicsScene(QObject *parent = 0);
virtual void mousePressEvent(QGraphicsSceneMouseEvent * mouseEvent);
signals:
public slots:
private:
int x, y;
};
#endif // GRAPHICSSCENE_H
Finally, to perform the image editions, the source file associated is:
graphicsscene.cpp
#include "graphicsscene.h"
#include <iostream>
using namespace std;
extern QImage Image_original, Image_modified;
GraphicsScene::GraphicsScene(QObject *parent) :
QGraphicsScene(parent)
{
this->setBackgroundBrush(Qt::gray);
}
void GraphicsScene::mousePressEvent(QGraphicsSceneMouseEvent * mouseEvent)
{
QGraphicsScene::mousePressEvent(mouseEvent);
if (mouseEvent->button()==Qt::LeftButton)
{
x=mouseEvent->scenePos().x();
y=mouseEvent->scenePos().y();
}
Image_modified=some_custom_image_editing_code(Image_original, x, y);
}
Ideally, I would like to execute the following action in MainWindow after the mousePressEvent is performed:
img->addPixmap(QPixmap::fromImage(Image_modified));
ui->preview->setScene(img);
I would highly appreciate any idea.
Since you want to click the item containing the pixmap it is not necessary to overwrite the QGraphicsScene mousePressEvent method, since you could click outside the image, it is better to overwrite that method in QGraphicsPixmapItem.
Instead of using extern to access the images, in Qt is better signals and slot, but only the classes that inherit from QObject can have these attributes, unfortunately QGraphicsPixmapItem does not inherit from this, but we can use it as interface.
graphicspixmapitem.h
#ifndef GRAPHICSPIXMAPITEM_H
#define GRAPHICSPIXMAPITEM_H
#include <QGraphicsPixmapItem>
#include <QObject>
class GraphicsPixmapItem : public QObject, public QGraphicsPixmapItem
{
Q_OBJECT
public:
explicit GraphicsPixmapItem(QObject *parent=0);
protected:
void mousePressEvent(QGraphicsSceneMouseEvent * event);
signals:
void newPixmap(const QPixmap p);
};
#endif // GRAPHICSPIXMAPITEM_H
graphicspixmapitem.cpp
#include "graphicspixmapitem.h"
#include <QGraphicsSceneMouseEvent>
GraphicsPixmapItem::GraphicsPixmapItem(QObject * parent):QObject(parent)
{
}
void GraphicsPixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
QPoint p = QPoint(event->pos().x(), event->pos().y());
QPixmap pix = pixmap();
if(!pix.isNull()){
QRect rect(QPoint(), pix.rect().size()/2);
rect.moveCenter(p);
QPixmap modified = pix.copy(rect);
modified = modified.scaled(pix.rect().size(), Qt::KeepAspectRatioByExpanding);
emit newPixmap(modified);
}
QGraphicsPixmapItem::mousePressEvent(event);
}
In the previous code we created the signal newPixmap, this connected it with a slot called onNewPixmap in MainWindow.
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "graphicsscene.h"
#include "graphicspixmapitem.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_actionOpen_triggered();
void onNewPixmap(const QPixmap pixmap);
private:
Ui::MainWindow *ui;
GraphicsPixmapItem *item;
GraphicsScene *scene;
QPixmap original_pixmap;
QPixmap new_pixmap;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QGraphicsView>
#include <QFileDialog>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
scene = new GraphicsScene(this);
ui->preview->setScene(scene);
item = new GraphicsPixmapItem;
/*ui->preview->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
ui->preview->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);*/
scene->addItem(item);
connect(item, &GraphicsPixmapItem::newPixmap, this, &MainWindow::onNewPixmap);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_actionOpen_triggered()
{
QString path = QDir::homePath();
QString filename=QFileDialog::getOpenFileName(this,
tr("Open Background"),
path,
tr("Images (*.png *.bmp *.jpg *.jpeg);;All files (*.*)"));
original_pixmap = QPixmap(filename);
item->setPixmap(original_pixmap);
}
void MainWindow::onNewPixmap(const QPixmap pixmap)
{
new_pixmap = pixmap;
QFile file("new_file.png");
file.open(QIODevice::WriteOnly);
new_pixmap.save(&file, "PNG");
}
In that slot as test I save the image in the folder where the executable is generated.
In your code I see that you use QImage, if you want to continue using it you can convert QPixmap to QImage with:
new_pixmap.toImage()
The complete example is found here

Application crashing after calling a method

I want to return a QString value from a function that exists in class called DB in my MainWindow class, but it always crashes the application.
db.h
#ifndef DB_H
#define DB_H
#include <QMainWindow>
#include <QtSQl>
#include <QSqlQuery>
#include <QSqlError>
#include <QObject>
#include <QDialog>
class DB : public QMainWindow
{
Q_OBJECT
public:
explicit DB(QWidget *parent = 0);
QString getDriver() const;
void setDriver(const QString &value);
private:
QString Driver="test";
signals:
public slots:
};
#endif // DB_H
and this is db.cpp
#include "db.h"
DB::DB(QWidget *parent) : QMainWindow(parent)
{
}
QString DB::getDriver() const
{
return Driver;
}
void DB::setDriver(const QString &value)
{
Driver = value;
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QDebug>
#include <QFileInfo>
#include <QDialog>
#include <QObject>
#include "Db.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
DB *conn;
private slots:
void on_pushButton_clicked();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
QString driverfromdb = conn->getDriver();
qDebug() << driverfromdb;
}
And this what happen when I click the pushbutton: image
You are declaring a pointer to DB class inside your MainWindow class
DB *conn;
but you never initialize it. At the moment it does not point to anything, so if you try to access it:
QString driverfromdb = conn->getDriver(); // boom, conn is somewhere in the void
The solution is simple, you need to create a DB object before using it, e.g.:
void MainWindow::on_pushButton_clicked()
{
conn = new DB();
QString driverfromdb = conn->getDriver();
qDebug() << driverfromdb;
}
or just let conn be a typical variable, not an pointer:
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
DB conn; // conn is created after creating MainWindow object
This way you have not to initialize anything and you will avoid crashes.
or:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent), conn(new DB),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete conn;
delete ui;
}

QT project QThread and C++ Thread limitations

mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QFileSystemModel>
#include <QThread>
#include <statusdialog.h>
#include <pbt.h>
#include <stdint.h>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
//void on_treeView_clicked(const QModelIndex &index);
void onCustomContextMenuTV(const QPoint &point);
void dirSize();
void getSelectedTreeItemSize();
void resultHandle(uint64_t);
signals:
void sizeCalculation(uint64_t);
private:
Ui::MainWindow *ui;
QString sPath;
QFileSystemModel *dirmodel;
QFileSystemModel *filemodel;
QAction *dirSizeAct;
statusDialog statusdialog;
};
#endif // MAINWINDOW_H
pbt.h
#ifndef STATUSDIALOG_H
#define STATUSDIALOG_H
#include <QDialog>
#include <stdint.h>
namespace Ui {
class statusDialog;
}
class statusDialog : public QDialog
{
Q_OBJECT
public:
explicit statusDialog(QWidget *parent = 0);
~statusDialog();
void setProgressbarMax(uint64_t);
private slots:
void on_pushButton_clicked();
private:
Ui::statusDialog *ui;
};
#endif // STATUSDIALOG_H
statusdialog.h
#ifndef STATUSDIALOG_H
#define STATUSDIALOG_H
#include <QDialog>
#include <stdint.h>
namespace Ui {
class statusDialog;
}
class statusDialog : public QDialog
{
Q_OBJECT
public:
explicit statusDialog(QWidget *parent = 0);
~statusDialog();
void setProgressbarMax(uint64_t);
private slots:
void on_pushButton_clicked();
private:
Ui::statusDialog *ui;
};
#endif // STATUSDIALOG_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
sPath = "C:/";
dirmodel = new QFileSystemModel(this);
dirmodel->setFilter(QDir::NoDotAndDotDot | QDir::AllDirs);
dirmodel->setRootPath(sPath);
ui->treeView->setModel(dirmodel);
ui->treeView->setContextMenuPolicy(Qt::CustomContextMenu);
connect(ui->treeView, SIGNAL(customContextMenuRequested(const QPoint &)), this, SLOT(onCustomContextMenuTV(const QPoint &)));
connect(this,SIGNAL(sizeCalculation(uint64_t)),this,SLOT(resultHandle(uint64_t)));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::resultHandle(uint64_t t_size)
{
statusdialog.setProgressbarMax(t_size);
}
void MainWindow::onCustomContextMenuTV(const QPoint &point)
{
dirSizeAct = new QAction(tr("Size"), this);
connect(dirSizeAct, SIGNAL(triggered()), this, SLOT(dirSize()));
QMenu contextMenu(this);
contextMenu.addAction(dirSizeAct);
QModelIndex index = ui->treeView->indexAt(point);
if (index.isValid()){
contextMenu.exec(ui->treeView->mapToGlobal(point));
}
}
void MainWindow::dirSize()
{
pBT pbt;
pbt.setFP(this->getSelectedTreeItemSize);
QThread thread1;
connect(&thread1,SIGNAL(started()),&pbt,SLOT(startThreadAction()));//Clone the object and it will not work, becouse it is QWidget
pbt.moveToThread(&thread1);
thread1.start();//Starts in the same thread
statusdialog.setModal(true);
statusdialog.exec();
}
void MainWindow::getSelectedTreeItemSize()
{
QModelIndex index = ui->treeView->selectionModel()->selectedIndexes().takeFirst();
QString dirPath = dirmodel->filePath(index);
QDirIterator it(dirPath, QStringList() << "*.*", QDir::Files, QDirIterator::Subdirectories);
uint64_t t_size = 0;
while (it.hasNext()) {
QFile myFile(it.next());
if (myFile.open(QIODevice::ReadOnly)){
t_size += myFile.size();
myFile.close();
}
}
emit sizeCalculation(t_size);
}
pbt.cpp
#include "pbt.h"
pBT::pBT(QObject *parent) : QObject(parent)
{
}
void pBT::startThreadAction()
{
this->fp1();
}
void pBT::setFP(void (*fp1)())
{
this->fp1 = fp1;
}
statusdialog.cpp
#include "statusdialog.h"
#include "ui_statusdialog.h"
statusDialog::statusDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::statusDialog)
{
ui->setupUi(this);
}
statusDialog::~statusDialog()
{
delete ui;
}
void statusDialog::on_pushButton_clicked()
{
this->close();
}
void statusDialog::setProgressbarMax(uint64_t size)
{
ui->progressBar->setMaximum(size);
}
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
To see more info of what I am doing you can look this topic(It is all about prograssbar and threaded execution of job). Theoretically I whant to run a member function of mainwindow(which use member variables of mainwindow) into new thread(the function is not static) inside slot dirSize()(also member slot of the same class mainwindow), but it seems with QThread it is necessary to create new class(no matter if I will inherit QThread class or use moveToThread function of QObject) and run that class in new thread. If I use C++ thread.h the function I run must be static, nevermind I have tried to create pBT class in which I have function pointer fp1 and public slot startThreadedAction, but when try to buil this error occures:
C:\Users\niki\Documents\EPsimple\mainwindow.cpp:54: error: C3867:
'MainWindow::getSelectedTreeItemSize': function call missing argument
list; use '&MainWindow::getSelectedTreeItemSize' to create a pointer
to member
I don`t want to create function pointer to static function! How can I fix this?