Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Qt 确定标签上的QRubberBand_Qt_Drawing_Selection_Rubber Band - Fatal编程技术网

Qt 确定标签上的QRubberBand

Qt 确定标签上的QRubberBand,qt,drawing,selection,rubber-band,Qt,Drawing,Selection,Rubber Band,奇怪的事情发生了:我需要在标签上使用橡皮筋。 这是我的密码: QRubberBand *rubberBand; QPoint mypoint; void MainWindow::mousePressEvent(QMouseEvent *event){ mypoint = event->pos(); rubberBand = new QRubberBand(QRubberBand::Rectangle, ui->label_2);//new rect

奇怪的事情发生了:我需要在标签上使用橡皮筋。 这是我的密码:

    QRubberBand *rubberBand;
    QPoint mypoint;

void MainWindow::mousePressEvent(QMouseEvent *event){

    mypoint = event->pos();
    rubberBand = new QRubberBand(QRubberBand::Rectangle, ui->label_2);//new rectangle band
    rubberBand->setGeometry(QRect(mypoint, ui->label_2->size()));
    rubberBand->show();
}

void MainWindow::mouseMoveEvent(QMouseEvent *event){
    rubberBand->setGeometry(QRect(mypoint, event->pos()).normalized());//Area Bounding
}

void MainWindow::mouseReleaseEvent(QMouseEvent *event){
    rubberBand->hide();// hide on mouse Release
    rubberBand->clearMask();
}
一切都正常,但只有一个问题——rubberBound开始比coursor低一点,或者设置在100-150px左右

我做错了什么

事件->位置()的坐标系与标签和橡皮筋的坐标系不同

您需要将
事件->pos()
映射到不同的坐标系以补偿偏移

编辑:下面是一个例子

// In your constructor set rubberBand to zero.
rubberBand = 0;

void MainWindow::mousePressEvent(QMouseEvent *event){

    mypoint = ui->label->mapFromGlobal(this->mapToGlobal(event->pos()));
    // mypoint = ui->label->mapFrom(this, event->pos());
    // mypoint = this->mapTo(ui->label, event->pos());
    if(rubberBand == 0) // You should add this to not have a memory leak
        rubberBand = new QRubberBand(QRubberBand::Rectangle, ui->label_2);//new rectangle band
    rubberBand->setGeometry(QRect(mypoint, ui->label_2->size()));
    rubberBand->show();
}
QRubberBand
描述中,它显示了如果在其鼠标事件被触发的小部件上使用它,它将如何实现。由于您在与其他窗口小部件鼠标事件不同的窗口小部件上使用它,因此必须映射坐标

希望能有所帮助。

事件->位置()与标签和橡皮筋的坐标系不同

您需要将
事件->pos()
映射到不同的坐标系以补偿偏移

编辑:下面是一个例子

// In your constructor set rubberBand to zero.
rubberBand = 0;

void MainWindow::mousePressEvent(QMouseEvent *event){

    mypoint = ui->label->mapFromGlobal(this->mapToGlobal(event->pos()));
    // mypoint = ui->label->mapFrom(this, event->pos());
    // mypoint = this->mapTo(ui->label, event->pos());
    if(rubberBand == 0) // You should add this to not have a memory leak
        rubberBand = new QRubberBand(QRubberBand::Rectangle, ui->label_2);//new rectangle band
    rubberBand->setGeometry(QRect(mypoint, ui->label_2->size()));
    rubberBand->show();
}
QRubberBand
描述中,它显示了如果在其鼠标事件被触发的小部件上使用它,它将如何实现。由于您在与其他窗口小部件鼠标事件不同的窗口小部件上使用它,因此必须映射坐标


希望这能有所帮助。

谢谢,但我仍然不知道如何将coords从主窗口转换到我的标签谢谢,但我仍然不知道如何将coords从主窗口转换到我的标签