E
E
Evgeny Petryaev2021-05-06 23:07:15
Qt
Evgeny Petryaev, 2021-05-06 23:07:15

Can't run code?

main.cpp

#include <iostream>
#include <QtWidgets/QApplication>
#include "MainWindow.h"

int main(int argc,char*argv[])
{
    QApplication a(argc, argv);
    MainWindow mw;
   
    return a.exec();
}

MainWindow.h
#pragma once
#include <QtWidgets/qmainwindow.h>
#include <QtGui/qpicture.h>
#include <QtGui/qimage.h>
#include <QtGui/qpainter.h>
#pragma comment(lib,"Qt5Core.lib")
#pragma comment(lib,"Qt5Widgets.lib")
#pragma comment(lib,"Qt5Gui.lib")
namespace Ui {
    class MainWindow;
}
class MainWindow : public QMainWindow
{
    Q_OBJECT
public:
    explicit MainWindow(QWidget* parent = 0);
    ~MainWindow();
protected:
    void paintEvent(QPaintEvent*); // пееопределение виртуальной функции
private:
    Ui::MainWindow* ui;
};

MainWindow.cpp
#include "MainWindow.h"
void MainWindow::paintEvent(QPaintEvent*)
{
    QImage img("sea.png"); // загружаем картинку
    QPainter painter(this); // определяем объект painter, который обеспечивает рисование
    painter.drawImage(0, 0, img.scaled(this->size())); // рисуем наше изображение от 0,0 и растягиваем по всему виджету
}

Gives an error message
Ошибка	LNK2001	неразрешенный внешний символ "public: virtual __cdecl MainWindow::~MainWindow(void)" ([email protected]@[email protected]).	
и
Ошибка	LNK2001	неразрешенный внешний символ "public: __cdecl MainWindow::MainWindow(class QWidget *)" ([email protected]@[email protected]@@@Z).

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Petryaev, 2021-05-07
@Gremlin92

I commented out the header in the QOBJECT class and the destructor, added the constructor implementation to MainWindow.cpp:

MainWindow::MainWindow(QWidget* parent)
    : QMainWindow(parent)
{
    setWindowTitle(tr("Window"));
    setGeometry(0, 0, 1000, 700);
}

and basically main.cpp added
#include <iostream>
#include <QtWidgets/QApplication>
#include "MainWindow.h"

#pragma comment(lib,"Qt5Widgets.lib")

int main(int argc,char*argv[])
{
    QApplication a(argc, argv);
    QWidget qw;
    //qw.show();
    MainWindow mw(&qw);
    mw.show();
    return a.exec();
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question