Why the image



  • I wanna load an image of 256*256 and put it in the center of widget, but no matter how I change the size of the window, only part of the image showed. What's wrong about that? Wish you could help me with this!
    0_1500252518346_QQ截图20170717084129.jpg

    //imagewarping.h
    
    #ifndef IMAGEWARPING_H
    #define IMAGEWARPING_H
    
    #include <QtWidgets/QMainWindow>
    #include "ui_imagewarping.h"
    #include "warpwidget.h"
    
    class ImageWarping : public QMainWindow
    {
    	Q_OBJECT
    
    public:
    	ImageWarping(QWidget *parent = 0);
    	~ImageWarping();
    
    	void Init();
    
    private:
    	Ui::ImageWarpingClass ui;
    
    	QMenu *menu_file;
    	QMenu *menu_edit;
    	QToolBar *toolbar_file;
    	QToolBar *toolbar_edit;
    	QAction *action_open;
    	QAction *action_save;
    	QAction *action_warp;
    	QAction *action_delete;
    
    	WarpWidget *warp_widegt;
    };
    
    #endif // IMAGEWARPING_H
    
    
    //imagewarping.cpp
    
    #include "imagewarping.h"
    
    ImageWarping::ImageWarping(QWidget *parent)
    	: QMainWindow(parent)
    {
    	ui.setupUi(this);
    
    	Init();
    }
    
    ImageWarping::~ImageWarping()
    {
    
    }
    
    void ImageWarping::Init()
    {
    	action_open = new QAction(QIcon("open.png"),tr("Open"),this);
    	action_save = new QAction(QIcon("save.png"),tr("Save"),this);
    	action_warp = new QAction(QIcon("warp.jpg"),tr("Warp"),this);
    	action_delete = new QAction(QIcon("delete.jpg"),tr("Delete"),this);
    
    	menu_file = menuBar()->addMenu(tr("&File"));
    	menu_file->setStatusTip(tr("file menu"));
    	menu_file->addAction(action_open);
    	menu_file->addAction(action_save);
    
    	menu_edit = menuBar()->addMenu(tr("&Edit"));
    	menu_edit->setStatusTip(tr("Edit"));
    	menu_edit->addAction(action_warp);
    	menu_edit->addAction(action_delete);
    
    	toolbar_file = addToolBar(tr("File"));
    	toolbar_file->addAction(action_open);
    	toolbar_file->addSeparator();
    	toolbar_file->addAction(action_save);
    	
    
    	toolbar_edit = addToolBar(tr("Warping"));
    	toolbar_edit->addAction(action_warp);
    	toolbar_edit->addSeparator();
    	toolbar_edit->addAction(action_delete);
    
    	warp_widegt = new WarpWidget();
    	setCentralWidget(warp_widegt);
    
    	connect(action_open,&QAction::triggered,warp_widegt,&WarpWidget::Open);
    	connect(action_save,&QAction::triggered,warp_widegt,&WarpWidget::Save);
    	connect(action_warp,&QAction::triggered,warp_widegt,&WarpWidget::Warp);
    	connect(action_delete,&QAction::triggered,warp_widegt,&WarpWidget::Delete);
    
    }
    
    //warpwidget.h
    
    #ifndef WARPWIDGET_H
    #define WARPWIDGET_H
    
    #include <QWidget>
    #include <QImage>
    #include <QPainter>
    #include <QFileDialog>
    #include <QMouseEvent>
    #include <vector>
    #include <math.h>
    #include <iostream>
    #include "ui_warpwidget.h"
    #include "Line.h"
    
    using namespace std;
    
    class WarpWidget : public QWidget
    {
    	Q_OBJECT
    
    public:
    	WarpWidget(QWidget *parent = 0);
    	~WarpWidget();
    
    	void paintEvent(QPaintEvent *);
    	void mousePressEvent(QMouseEvent *event);
    	void mouseMoveEvent(QMouseEvent *event);
    	void mouseReleaseEvent(QMouseEvent *event);
    
    	void Open();
    	void Save();
    	void Warp();
    	void Reset();
    	void Delete();
    
    	double Di_func(QPoint p,QPoint pi);
    	double Delta(QPoint p,QPoint pi);
    	double Wi_func(QPoint p,QPoint pi);
    	QPoint Fi_fun(QPoint p,int i);
    	QPoint F_Func(QPoint p);
    
    private:
    	Ui::WarpWidget ui;
    
    	bool draw_status;
    
    	QImage *ptr_image;
    	QImage *ptr_image_backup;
    
    	QPoint m_StartPoint;
    	QPoint m_EndPoint;
    	double u;
    	vector<Line*> line_array;
    };
    
    #endif // WARPWIDGET_H
    
    
    //warpwidget.cpp
    
    #include "warpwidget.h"
    
    WarpWidget::WarpWidget(QWidget *parent)
    	: QWidget(parent)
    {
    	ui.setupUi(this);
    
    	ptr_image = new QImage();
    	ptr_image_backup = new QImage();
    
    	draw_status = false;
    }
    
    WarpWidget::~WarpWidget()
    {
    	for (int i=0;i<line_array.size();i++)
    	{
    		if (line_array[i])
    		{
    			delete line_array[i];
    			line_array[i] = NULL;
    		}
    	}
    }
    
    void WarpWidget::Open()
    {
    	QString fileName = QFileDialog::getOpenFileName(this,tr("Read Image"),".",tr("Image(*.bmp *.png *.jpg)"));
    
    	if (!fileName.isEmpty())
    	{
    		ptr_image->load(fileName);
    		ptr_image_backup->load(fileName);
    	}
    
    	update();
    }
    
    void WarpWidget::Save()
    {
    	QString fileName = QFileDialog::getSaveFileName(this,tr("Save Image"),".",tr("Image(*.bmp,*.png,*.jpg)"));
    }
    
    void WarpWidget::Warp()
    {
    
    }
    
    void WarpWidget::Reset()
    {
    	*ptr_image = *ptr_image_backup;
    
    	update();
    }
    
    void WarpWidget::Delete()
    {
    	if (!line_array.empty())
    	{
    		line_array.pop_back();
    	}
    	
    }
    
    void WarpWidget::mousePressEvent(QMouseEvent *event)
    {
    	if (Qt::LeftButton == event->button())
    	{
    		draw_status = true;
    
    		m_StartPoint = m_EndPoint = event->pos();
    	}
    }
    
    void WarpWidget::mouseMoveEvent(QMouseEvent *event)
    {
    	if (draw_status)
    	{
    		m_EndPoint = event->pos();
    	}
    }
    
    void WarpWidget::mouseReleaseEvent(QMouseEvent *event)
    {
    	Line *current_line = NULL;
    	current_line = new Line(m_StartPoint,m_EndPoint);
    	line_array.push_back(current_line);
    	draw_status = false;
    }
    
    void WarpWidget::paintEvent(QPaintEvent *)
    {
    	QPen pen;
    	QPainter painter(this);
    	painter.begin(this);
    
    	painter.setBrush(Qt::lightGray);
    	QRect back_rect(0,0,width(),height());
    	painter.drawRect(back_rect);
            //*Put the image in the center of the widget*
    	QRect image_rect((width()-ptr_image->width()/2),(height()-ptr_image->height())/2,width(),height());
    	QRect source(0,0,100,100);
    	painter.drawImage(image_rect,*ptr_image);
    	
    
    	pen.setWidth(5);	
    	pen.setColor(Qt::darkCyan);
    	painter.setPen(pen);
    
    	for (int i=0;i<line_array.size();i++)
    	{
    		line_array[i]->Draw(painter);
    	}
    
    	if (draw_status)
    	{	
    		painter.drawLine(m_StartPoint,m_EndPoint);
    	}
    
    	painter.end();
    
    	update();
    }
    
    double WarpWidget::Di_func(QPoint p,QPoint pi)
    {
    	return ((p.rx()-pi.rx())*(p.rx()-pi.rx())+(p.ry()-pi.ry())*(p.ry()-pi.ry()));
    }
    
    double WarpWidget::Delta(QPoint p,QPoint pi)
    {
    	return pow(1/Di_func(p,pi),u/2.0);
    }
    
    double WarpWidget::Wi_func(QPoint p,QPoint pi)
    {
    	double sum = 0;
    	if (p == pi)
    	{
    		return 1;
    	}
    	else
    	{
    		for (int i=0;i<line_array.size();i++)
    		{
    			sum += Delta(p,line_array[i]->getStartPoint());
    		}
    		return Delta(p,pi)/sum;
    	}
    }
    
    QPoint WarpWidget::Fi_fun(QPoint p,int i)
    {
    	QPoint result = line_array[i]->getEndPoint();
    
    	result.rx() += p.rx() - line_array[i]->getEndPoint().rx();
    	result.ry() += p.ry() - line_array[i]->getEndPoint().ry();
    
    	return result;
    }
    
    QPoint WarpWidget::F_Func(QPoint p)
    {
    	QPoint result(0,0);
    	for (int i=0;i<line_array.size();i++)
    	{
    		result.rx() += Wi_func(p,line_array[i]->getStartPoint()) * Fi_fun(p,i).rx();
    		result.ry() += Wi_func(p,line_array[i]->getStartPoint()) * Fi_fun(p,i).ry();
    	}
    
    	return result;
    }
    
    
    


  • @Geng.Y said in Why the image:

    WarpWidget

    warp_widegt = new WarpWidget();
    QHBoxLayout* hbox = new QHBoxLayout(this);
    hbox->addWidget(warp_widegt);
    this->centreWidget()->setLayout( hbox );
    setCentralWidget( centreWidget() );



  • There seems to be another mistake here.
    0_1500262805672_QQ截图20170717113859.jpg



  • @Geng.Y

    warp_widegt = new WarpWidget();
    QHBoxLayout* hbox = new QHBoxLayout(this);
    hbox->addWidget(warp_widegt);
    QWidget* centreWidget = new QWidget();
    centreWidget->setLayout( hbox );
    setCentralWidget( centreWidget );



  • It still doesn't work. Thank you!
    0_1500264410818_QQ截图20170717120641.jpg



  • @Geng-Y ,

    Have you tried setAlignment?



  • Not yet. I'm new to Qt, but I'll try!


  • Moderators

    @Geng.Y said in Why the image:

    QRect image_rect((width()-ptr_image->width()/2),(height()-ptr_image->height())/2,width(),height());
    QRect source(0,0,100,100);
    painter.drawImage(image_rect,*ptr_image);

    Try this instead:

    int x = (width()-ptr_image->width()) / 2;
    int y = (height()-ptr_image->height())/2;
    painter.drawImage( QPoint(x,y),*ptr_image );
    

    at least you have a misplaced ) in your code.



  • @raven-worx Thank you very much, and I find that I've neglected ()!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.