qt 多选graphicsitem可以获取到共选了多少个吗
时间: 2023-02-15 14:42:35 浏览: 48
qt 的 graphicsitem 可以通过使用 QGraphicsScene 类的 selectedItems() 函数来获取已选择的 graphicsitem。此函数返回一个 QList,包含当前已选择的所有 graphicsitem。你可以使用 QList 的 size() 函数来获取已选择的 graphicsitem 的数量。例如:
```
QList<QGraphicsItem *> selectedItems = scene->selectedItems();
int numSelected = selectedItems.size();
```
这样你就可以得到已选择的 graphicsitem 的数量。
相关问题
用Qt的GraphicsItem 自绘一个QLinedit 并且可以输入文本
以下是一个用Qt的GraphicsItem自绘的QLinedit并且可以输入文本的示例代码:
```
#include <QGraphicsItem>
#include <QPainter>
#include <QLineEdit>
class CustomLineEdit : public QGraphicsItem
{
public:
CustomLineEdit(QGraphicsItem *parent = nullptr)
: QGraphicsItem(parent)
{
setFlag(ItemIsFocusable);
setAcceptHoverEvents(true);
m_edit = new QLineEdit();
m_edit->setFrame(false);
m_edit->setAttribute(Qt::WA_TranslucentBackground);
m_edit->setStyleSheet("QLineEdit { background-color: transparent; }");
m_edit->setAlignment(Qt::AlignCenter);
}
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr) override
{
Q_UNUSED(option);
Q_UNUSED(widget);
QRectF rect = boundingRect();
painter->setPen(Qt::NoPen);
painter->setBrush(Qt::white);
painter->drawRect(rect);
painter->setPen(Qt::black);
painter->drawText(rect, Qt::AlignCenter, m_edit->text());
}
QRectF boundingRect() const override
{
return QRectF(0, 0, 100, 20);
}
void keyPressEvent(QKeyEvent *event) override
{
if (event->key() == Qt::Key_Backspace)
{
QString text = m_edit->text();
if (!text.isEmpty())
{
text.chop(1);
m_edit->setText(text);
update();
}
}
else
{
m_edit->keyPressEvent(event);
update();
}
}
void focusInEvent(QFocusEvent *event) override
{
m_edit->setFocus();
update();
}
void focusOutEvent(QFocusEvent *event) override
{
m_edit->clearFocus();
update();
}
void hoverEnterEvent(QGraphicsSceneHoverEvent *event) override
{
setCursor(Qt::IBeamCursor);
update();
}
void hoverLeaveEvent(QGraphicsSceneHoverEvent *event) override
{
unsetCursor();
update();
}
private:
QLineEdit *m_edit;
};
```
这里我们定义了一个CustomLineEdit类,继承自QGraphicsItem,然后在paint()函数中绘制了一个白色的矩形和黑色的文本,文本内容是从QLineEdit中获取的。在keyPressEvent()函数中,我们监听了键盘事件,如果是退格键,则从QLineEdit中删除最后一个字符,如果是其他键,则将事件传递给QLineEdit处理。在focusInEvent()和focusOutEvent()函数中,我们将焦点转移到QLineEdit上,以便能够输入文本。在hoverEnterEvent()和hoverLeaveEvent()函数中,我们设置了鼠标悬停时的光标形状。
用Qt的GraphicsItem 自绘一个QLinedit
以下是一个使用Qt的GraphicsItem自绘QLinedit的示例代码:
```cpp
#include <QGraphicsItem>
#include <QPainter>
#include <QLineEdit>
class CustomLineEdit : public QGraphicsItem
{
public:
CustomLineEdit(QGraphicsItem *parent = nullptr)
: QGraphicsItem(parent)
{
m_lineEdit.setParent(nullptr);
m_lineEdit.installEventFilter(this);
}
QRectF boundingRect() const override
{
return QRectF(0, 0, 100, 20);
}
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override
{
Q_UNUSED(option)
Q_UNUSED(widget)
painter->setPen(Qt::black);
painter->drawRect(boundingRect());
QRectF textRect = boundingRect().adjusted(2, 2, -2, -2);
painter->setFont(m_lineEdit.font());
painter->drawText(textRect, m_lineEdit.text());
}
bool eventFilter(QObject *obj, QEvent *event) override
{
if (obj == &m_lineEdit && event->type() == QEvent::TextEdited)
{
update();
return true;
}
return false;
}
private:
QLineEdit m_lineEdit;
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QGraphicsScene scene;
CustomLineEdit *customLineEdit = new CustomLineEdit();
scene.addItem(customLineEdit);
customLineEdit->setPos(20, 20);
QGraphicsView view(&scene);
view.show();
return app.exec();
}
```
在这个示例中,我们创建了一个名为CustomLineEdit的自定义GraphicsItem,它继承自QGraphicsItem。在CustomLineEdit的构造函数中,我们创建了一个QLineEdit,并使用eventFilter来捕获TextEdit的文本编辑事件,并在其上绘制文本。在paint函数中,我们使用QPainter来绘制一个矩形和文本。CustomLineEdit的boundingRect函数返回矩形的大小。最后,我们将CustomLineEdit添加到QGraphicsScene中并在QGraphicsView中显示它。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)