
Originally Posted by
wysota
I wouldn't "disable" mouse events in the scene. I would just gave a flag set in the scene according to the state of the list box. Then if the flag is set (something is selected) I would do the main thing the event is supposed to do, otherwise I would just emit a signal with mouse click position so that you can connect to it and set up the labels.
I would also do it that way but I have no idea how to work it out in qt. My problem is I don't know how to communication between the 2 widgets. I am still new to qt.
below are codes I did but got stuck halfway.
Mainwindow.h
....
private:
mapscene *scene;
....
private:
QLabel *posX;
QLabel *posY;
QListWidget *area;
mapscene *scene;
To copy to clipboard, switch view to plain text mode
Mainwindow.cpp
MainWindow
::MainWindow(QWidget *parent
){
posX->setMinimumWidth(50);
posX
->setFrameShape
(QFrame::Box);
posY->setMinimumWidth(50);
posY
->setFrameShape
(QFrame::Box);
area->setMaximumWidth(180);
area->setMinimumHeight(300);
//assume all items added
scene = new mapscene;
scene->setSceneRect(0, 0, 600, 600);
}
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
posX = new QLabel;
posX->setMinimumWidth(50);
posX->setFrameShape(QFrame::Box);
posY = new QLabel;
posY->setMinimumWidth(50);
posY->setFrameShape(QFrame::Box);
area = new QListWidget;
area->setMaximumWidth(180);
area->setMinimumHeight(300);
//assume all items added
scene = new mapscene;
scene->setSceneRect(0, 0, 600, 600);
}
To copy to clipboard, switch view to plain text mode
mapscene.h
{
public:
mapscene();
protected:
private:
};
class mapscene : public QGraphicsScene
{
public:
mapscene();
protected:
void mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent);
void mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent);
private:
QGraphicsEllipseItem *item;
};
To copy to clipboard, switch view to plain text mode
mapscene.cpp
const int ellsize = 5;
mapscene::mapscene()
{
item->setBrush(Qt::red);
addItem(item);
}
{
//check status of listbox. How to check?
removeItem(item);
item->setRect(mouseEvent->scenePos().x(),mouseEvent->scenePos().y(),ellsize, ellsize);
addItem(item);
//update posX and posY (Qlabel of Mainwindow). How to update?
}
{
}
const int ellsize = 5;
mapscene::mapscene()
{
item = new QGraphicsEllipseItem;
item->setBrush(Qt::red);
addItem(item);
}
void mapscene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
//check status of listbox. How to check?
removeItem(item);
item->setRect(mouseEvent->scenePos().x(),mouseEvent->scenePos().y(),ellsize, ellsize);
addItem(item);
//update posX and posY (Qlabel of Mainwindow). How to update?
QGraphicsScene::mousePressEvent(mouseEvent);
}
void mapscene::mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
QGraphicsScene::mouseReleaseEvent(mouseEvent);
}
To copy to clipboard, switch view to plain text mode
Bookmarks