0
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMouseEvent>
#include <QDebug>
#include "my_qlabel.h"
#include<QTimer>
int px;
int py;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
tmrTimer = new QTimer(this);
connect(tmrTimer,SIGNAL(timeout()),this,SLOT(showthepositionofmouse()));
tmrTimer->start(20);
}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::showthepositionofmouse()
{
ui->plainTextEdit->appendPlainText(QString(" x ,  y = ")+QString::number(px)+QString::number(py));
}
void my_qlabel::mousePressEvent(QMouseEvent *event)
{ 
if (event->button()==Qt::RightButton){
    px = event->x();
    py = event->y();
}
}

我想显示鼠标点击的位置,我ui->plainTextEdit->appendPlainText(QString(" x , y = ")+QString::number(px)+QString::number(py));用来显示这个位置。虽然我点击鼠标,它只显示x,y = 0 0。为什么?

4

2 回答 2

0

在中访问的pyand是 的成员变量,对是不可见的。pxmy_qlabel::mousePressEventmy_qlabelMainWindow::showthepositionofmouse

如果您希望它们可见,则应使用信号和插槽发送它们。

http://qt-project.org/doc/qt-4.8/signalsandslots.html

这是实现它的一种方法:

在 my_qlabel.h 中有这样的内容:

class my_qlabel : public QLabel
{
    Q_OBJECT

signals:
    void right_click_xy(int x, int y);
    // ...constructor and other functions

public slots:
    void mousePressEvent(QMouseEvent *event)
    { 
        int px, py;
        if (event->button()==Qt::RightButton){
            px = event->x();
            py = event->y();
            emit right_click_xy(px, py);
        }
    }
}

在 mainwindow.h 中有这样的东西:

class MainWindow : public QMainWindow
{
    Q_OBJECT

    // ... constructor and other functions
public slots:
    void on_display_click_xy(int px, int py)
    {
        qDebug() << "Received xy over signal slot:" << px << py;
        ui->plainTextEdit->appendPlainText(QString(" x ,  y = ")+QString::number(px)+QString::number(py));
    }
}

在您的 MainWindow 构造函数中放置以下内容:

QObject::connect(ui->my_qlabel_instance, SIGNAL(right_click_xy(int,int)), 
    this, SLOT(on_display_click_xy(int, int)));

另一种执行此操作的替代方法,但不是 kosher,是深入了解您的ui对象和访问pxpy方式。

qDebug() << ui->my_qlabel_instance->py;

但这并不那么优雅,也没有以同样的方式发出信号。

另一个需要研究的想法是使用一个QPoint对象而不是两个ints。

希望有帮助。

于 2013-05-23T19:22:37.490 回答
0

整个想法很糟糕,您必须在 my_qlabel 类中通过信号和插槽处理这个事件,而不是在它之外。我会建议这样的事情(用鼠标点击的坐标发出信号):

头文件 my_qlabel.h:

#ifndef MY_QLABEL_H
#define MY_QLABEL_H

#include <QLabel>
#include <QPoint>
#include <QEvent>

class my_qlabel : public QLabel
{
Q_OBJECT
public:
    my_qlabel( const QString & text="", QWidget * parent = 0 );

signals:
    void clicked(QPoint pos);
protected:
    void mouseReleaseEvent ( QMouseEvent * event );
};

#endif // MY_QLABEL

来源 my_qlabel.cpp:

#include "my_qlabel.h"
#include <QMouseEvent>
my_qlabel::my_qlabel( const QString & text, QWidget * parent )
:QLabel(parent)
{
    setText(text);
}
void my_qlabel::mouseReleaseEvent ( QMouseEvent * event )
{
    emit clicked(event->pos());
}

主窗口.h:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include "my_qlabel.h"
#include <QtGui/QWidget>

class MainWindow : public QWidget
{
Q_OBJECT

public:
    MainWindow(QWidget *parent = 0);
    ~MainWindow();
public slots:
    void showthepositionofmouse(QPoint pos);
};
#endif // MAINWINDOW_H

和 mainwindow.cpp:

#include "mainwindow.h"
#include <QDebug>
#include <QPoint>
#include <QHBoxLayout>

MainWindow::MainWindow(QWidget *parent) :
QWidget(parent)
{
    my_qlabel* label=new my_qlabel("Text of my label",this);
    label->setAlignment(Qt::AlignCenter);
    QGridLayout *lay=new QGridLayout(this);
    this->setLayout(lay);
    lay->addWidget(label,0,0,1,1);

    connect(label,SIGNAL(clicked(QPoint)),this,SLOT(showthepositionofmouse(QPoint)));
}

MainWindow::~MainWindow()
{

}

void MainWindow::showthepositionofmouse(QPoint pos)
{
    qDebug()<< "Clicked, position="<<pos;
}
于 2013-05-23T19:30:22.523 回答