Help with this task

I need to write a program in Qt. Put a picture on the main window. And create a new file, write some text to the file, and display the recorded text on the screen by copying. You need to implement two buttons. The first to open the file, the second to copy and display. Help with the code. I put the picture, but there are problems with the implementation of the buttons.

Last edited on
if you put things on top of each other (eg the image is over the button) things can misbehave. A few of the issues include graphics (can't see the button, is behind other widget) or click problems (can see but not click, because is behind other widget) and similar things.

If its that just give more room to keep them away from each other, if its something else, explain in many words what is going on.
I did everything regarding the placement of the picture and buttons. There is a problem to assign buttons to the corresponding code actions and a problem how to open the file
Try the example?
All the ‘I have some problem’ solution providers were sacked recently because they had no idea what they were supposed to do.
I have code. And I need insert a picture in mainwindow.

#include "Widget.h"
#include <QPixmap>

Widget::Widget(QWidget *parent) : QDialog(parent) {



auto *lay = new QVBoxLayout;
auto *btn1 = new QPushButton("Open");
auto *btn2 = new QPushButton("Show");
edit = new QPlainTextEdit;
edit->setVisible(false);
lay->addWidget(btn1);
lay->addWidget(btn2);
lay->addWidget(edit);
setLayout(lay);
this->layout()->setSizeConstraint(QLayout::SetFixedSize);
connect(btn1, &QPushButton::clicked, this, &Widget::openFile);
connect(btn2, &QPushButton::clicked, this, &Widget::showFile);

}

void Widget::openFile() {
QFile file(QFileDialog::getOpenFileName(this, "Open txt file", "./", "TXT Files (*.txt)"));
file.open(QIODevice::ReadOnly);
while (!file.atEnd()) {
lines.push_back(file.readLine());
}
file.close();
}

void Widget::showFile() {
edit->setPlainText(lines.join(""));
edit->setVisible(true);
}
@OP
FWIW this follows your snippet as a complete project which runs.

.pro file
1
2
3
4
5
QT       += core widgets
TARGET = ImagesAndText
TEMPLATE = app
SOURCES += main.cpp mainwindow.cpp
HEADERS += mainwindow.h


mainwindow.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QPushButton>
#include <QFileDialog>
#include <QPlainTextEdit>
#include <QLabel>
#include <QTextStream>

namespace Ui
{
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT
public:
    explicit MainWindow(QWidget *parent = nullptr);
private slots:
    void images_click();
    void text_click();

private:
    QPushButton* btn_images;
    QPushButton* btn_text;

    QPlainTextEdit* edit;
    QPixmap* picture;
    QLabel* label;
    QString fileName;
};
#endif // MAINWINDOW_H 


main.cpp
1
2
3
4
5
6
7
8
9
10
#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    MainWindow mainWindow;
    mainWindow.showMaximized();
    return app.exec();
}


mainwindow.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include "mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    edit = new QPlainTextEdit(this);
    edit->resize(500,500);
    edit->hide();

    label = new QLabel(this);
    label->resize(500,500);
    label->hide();

    btn_images = new QPushButton("Image Files", this);
    btn_text = new QPushButton("Text Files", this);

    btn_images->setGeometry(QRect(QPoint(0, 600), QSize(200, 50)));
    btn_text->setGeometry(QRect(QPoint(0, 650), QSize(200, 50)));


    connect(btn_images, &QPushButton::clicked, this, &MainWindow::images_click);
    connect(btn_text, &QPushButton::clicked, this, &MainWindow::text_click);
}

void MainWindow::images_click()
{
    fileName = QFileDialog::getOpenFileName(this,
                                            tr("Open image files"), "",
                                            tr("Images (*.bmp *.jpg);;All Files (*)"));

    edit->hide();
    label->show();

    QPixmap pix(fileName);
    label->setPixmap(pix);
}

void MainWindow::text_click()
{
    edit->show();
    label->hide();
    fileName = QFileDialog::getOpenFileName(this,
                                            tr("Open text files"), "",
                                            tr("Documents (*.txt *.odt);;All Files (*)"));

    QFile file(fileName);
    file.open(QIODevice::ReadOnly);
    QTextStream stream(&file);
    QString content = stream.readAll();
    file.close();
    edit->setPlainText(content);
}
Topic archived. No new replies allowed.