What's wrong with this information?
Solved
General and Desktop
-
I've checked this code for an hour and couldn't find anything wrong. Wish you can help me with this!
//annotation.h #ifndef ANNOTATION_H #define ANNOTATION_H #include <QtWidgets/QMainWindow> #include "ui_annotation.h" #include "imagewidget.h" class Annotation : public QMainWindow { Q_OBJECT public: Annotation(QWidget *parent = 0); ~Annotation(); void Init(); private: Ui::AnnotationClass ui; QMenu *menu_file; QToolBar *toolbar_open; QToolBar *toolbar_save; QAction *action_open; QAction *action_save; ImageWidget *image_widget; }; #endif // ANNOTATION_H
//annotation.cpp #include "annotation.h" Annotation::Annotation(QWidget *parent) : QMainWindow(parent) { ui.setupUi(this); Init(); } Annotation::~Annotation() { } void Annotation::Init() { menu_file = menuBar()->addMenu(tr("&File")); action_open = new QAction(tr("Open"),this); action_save = new QAction(tr("Save"),this); toolbar_open = addToolBar(tr("Open")); toolbar_save = addToolBar(tr("Save")); menu_file->addAction(action_open); menu_file->addAction(action_save); toolbar_open->addAction(action_open); toolbar_save->addAction(action_save); connect(action_open,&QAction::triggered,image_widget,&ImageWidget::Open); connect(action_save,&QAction::triggered,image_widget,&ImageWidget::Save); image_widget = new ImageWidget(); setCentralWidget(image_widget); }
//imagewidget.h #ifndef IMAGEWIDGET_H #define IMAGEWIDGET_H #include <QWidget> #include <QImage> #include <QPainter> #include <QFileDialog> #include "ui_imagewidget.h" class ImageWidget : public QWidget { Q_OBJECT public: ImageWidget(QWidget *parent = 0); ~ImageWidget(); public slots: void Open(); void Save(); void ReadXml(); void paintEvent(QPaintEvent *); private: Ui::ImageWidget ui; QImage *ptr_image; }; #endif // IMAGEWIDGET_H
//imagewidget.cpp #include "imagewidget.h" ImageWidget::ImageWidget(QWidget *parent) : QWidget(parent) { ui.setupUi(this); ptr_image = new QImage(); } ImageWidget::~ImageWidget() { } void ImageWidget::Open() { QString fileName = QFileDialog::getOpenFileName(this,tr("Read Image"),".",tr("Image(*.bmp *.png *.jpg)")); if (!fileName.isEmpty()) { ptr_image->load(fileName); } update(); } void ImageWidget::Save() { } void ImageWidget::ReadXml() { } void ImageWidget::paintEvent(QPaintEvent *) { QPainter painter(this); QRect image_rect(0,0,ptr_image->width(),ptr_image->height()); painter.begin(this); painter.drawImage(image_rect,*ptr_image); update(); }
-
@Geng.Y said in What's wrong with this information?:
connect(action_open,&QAction::triggered,image_widget,&ImageWidget::Open);
connect(action_save,&QAction::triggered,image_widget,&ImageWidget::Save);image_widget = new ImageWidget();
You are connecting to a dangling pointer here. Interchange image_widget definition and connect() calls.