简体   繁体   中英

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

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. PS: 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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM