Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. What's wrong with this information?
Qt 6.11 is out! See what's new in the release blog

What's wrong with this information?

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 1.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    Geng.Y
    wrote on last edited by
    #1

    I've checked this code for an hour and couldn't find anything wrong. Wish you can help me with this!

    0_1500720902189_QQ截图20170722185212.jpg

    //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();
    
    }
    
    1 Reply Last reply
    0
    • T Offline
      T Offline
      taedium
      wrote on last edited by taedium
      #2

      @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.

      1 Reply Last reply
      3
      • G Offline
        G Offline
        Geng.Y
        wrote on last edited by
        #3

        Thank you very much! I was so careless!

        1 Reply Last reply
        0
        • T Offline
          T Offline
          taedium
          wrote on last edited by taedium
          #4

          Also calling update() inside paintEvent will cause an infinte loop.

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved