Whats' wrong with this "connect“ ?



  • I wanna connect several signals with several slots, but why the first "connect" works while the second one doesn't? Wish you can help me with this!
    0_1499910587784_20170713094802.png

    //minidraw.h
    
    #ifndef MINIDRAW_H
    #define MINIDRAW_H
    
    #include <QtWidgets/QMainWindow>
    #include "ui_minidraw.h"
    #include "viewwidget.h"
    #include <QMessageBox>
    
    class MiniDraw : public QMainWindow
    {
    	Q_OBJECT
    
    public:
    	MiniDraw(QWidget *parent = 0);
    	~MiniDraw();
    
    	void Init();
    	void About();
    private:
    	Ui::MiniDrawClass ui;
    
    	QMenu *pMenu1,*pMenu2;
    	QAction *pAction0,*pAction1,*pAction2,*pAction3,*pAction4,*pAction5,*pAction6;
    	QToolBar *pToolbar1,*pToolbar2,*pToolbar3,*pToolbar4,*pToolbar5,*pToolbar6;
    
    	ViewWidget *view_widget_;
    	
    };
    
    #endif // MINIDRAW_H
    
    
    //minidraw.cpp
    
    #include "minidraw.h"
    
    
    MiniDraw::MiniDraw(QWidget *parent)
    	: QMainWindow(parent)
    {
    	ui.setupUi(this);
    
    	Init();
    
    }
    
    MiniDraw::~MiniDraw()
    {
    
    }
    
    void MiniDraw::Init()
    {
    	pMenu1 = menuBar()->addMenu(tr("&Menu"));
    	pMenu2 = menuBar()->addMenu((tr("&About")));
    
    	pAction0 = new QAction(tr("&About"),this);
    	pAction1 = new QAction(tr("&Line"),this);
    	pAction2 = new QAction(tr("&Rectangle"),this);
    	pAction3 = new QAction(tr("Ellipse"),this);
    	
    	pToolbar1 = addToolBar(tr("&Line"));
    	pToolbar2 = addToolBar(tr("&Rectangle"));
    	pToolbar3 = addToolBar(tr("&Ellipse"));
    
    	pToolbar1->addAction(pAction1);
    	pToolbar2->addAction(pAction2);
    	pToolbar3->addAction(pAction3);
    
    	pMenu1->addAction(pAction1);
    	pMenu1->addAction(pAction2);
    	pMenu1->addAction(pAction3);
    	pMenu2->addAction(pAction0);
    	
    	connect(pAction0,&QAction::triggered,this,&MiniDraw::About);
    	connect(pAction1,&QAction::triggered,this,&ViewWidget::setType_Line); // **It dosen't work!!!**
    	view_widget_ = new ViewWidget();
    	setCentralWidget(view_widget_);
    }
    
    void MiniDraw::About()
    {
    	QMessageBox::about(this,tr("About"),tr("Demo of MiniDraw\n  by Geng.Y"));
    }
    
    //viewwidget.h
    
    #ifndef VIEWWIDGET_H
    #define VIEWWIDGET_H
    
    #include <QWidget>
    #include <QMouseEvent>
    #include <QPainter>
    #include <vector>
    #include "ui_viewwidget.h"
    #include "Figure.h"
    
    using namespace std;
    
    enum FigureType
    {
    	kDefault = 0,
    	kLine = 1,
    	kEllipse = 2,
    };
    
    class ViewWidget : public QWidget
    {
    	Q_OBJECT
    
    public:
    	ViewWidget(QWidget *parent = 0);
    	~ViewWidget();
    
    	void mousePressEvent(QMouseEvent *event);
    	void mouseMoveEvent(QMouseEvent * event);
    	void mouseReleaseEvent(QMouseEvent* event);
    	void paintEvent(QPaintEvent *);
    
    	void setType_Line();
    	void setType_Ellipse();
    
    private:
    	Ui::ViewWidget ui;
    
    	bool draw_status;
    	QPoint mStartpoint;
    	QPoint mEndPoint;
    
    	vector<Figure*> figure_array;
    	FigureType figure_type;
    	
    };
    
    #endif // VIEWWIDGET_H
    
    
    //viewwidget.cpp
    
    #include "viewwidget.h"
    
    ViewWidget::ViewWidget(QWidget *parent)
    	: QWidget(parent)
    {
    	ui.setupUi(this);
    
    	draw_status = false;
    }
    
    ViewWidget::~ViewWidget()
    {
    	for (int i=0;i<figure_array.size();i++)
    	{
    		if (figure_array[i])
    		{
    			delete figure_array[i];
    			figure_array[i] = NULL;
    		}
    	}
    }
    
    void ViewWidget::mousePressEvent(QMouseEvent *event)
    {
    	if (Qt::LeftButton==event->button())
    	{
    		draw_status = true;
    
    		mStartpoint = mEndPoint = event->pos();
    	}
    }
    
    void ViewWidget::mouseMoveEvent(QMouseEvent * event)
    {
    	if (draw_status)
    	{
    		mEndPoint = event->pos();
    	}
    }
    
    void ViewWidget::mouseReleaseEvent(QMouseEvent *evnet)
    {
    	draw_status = false;
    }
    
    void ViewWidget::paintEvent(QPaintEvent *)
    {
    	QPainter painter(this);
    	painter.begin(this);
    	//painter.drawLine(mStartpoint,mEndPoint);
    	//painter.drawEllipse(mStartpoint.rx(),mStartpoint.ry(),mEndPoint.rx()-mStartpoint.ry(),mEndPoint.ry()-mStartpoint.ry());
    	painter.end();
    
    	update();
    }
    
    void ViewWidget::setType_Line()
    {
    	figure_type = kLine;
    }
    
    void ViewWidget::setType_Ellipse()
    {
    	figure_type = kEllipse;
    }
    


  • @Geng-Y ,
    

    You should create the ViewWidget object before the connect statement,

    view_widget_ = new ViewWidget();
    connect(pAction0,&QAction::triggered,this,&MiniDraw::About);
    connect(pAction1,&QAction::triggered,view_widget_,&ViewWidget::setType_Line);



  • Thank you very much!



  • @Geng.Y

    Hi,

    Please mark your issue as solved.


Log in to reply
 

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