updating: “ https://github.com/Heydullsam/paste_pic.git“
1 全屏截图
- 设置图标
- QSystemTrayIcon sysMenu.setIcon() .show()
- QCursor::pos()定位鼠标位置
- QRect确定矩形
- QMenu.addAction添加小组件的功能
menu->addAction("截图",this,SLOT(grapScreen()));
可以直接使用槽函数
- Qpainter.drawpixmap绘制,方便调用重写paintEvent
2 选区截图
- 实现了selectRectWidget类,通过重写鼠标事件实现部分区域的抓取
- 通过update()和drawRect实现选中框的更新
- 原窗口逻辑中加入resize,避免框选图片被强制拉长
3 实现截图的拖动功能
- 复用了之前DragFiler,加入了右键关闭功能
- 安装DragFilter
4 实现快捷键
- 引入第三方库QHotkey, 解决了没有主窗口无法触发截图快捷键的问题
+third_party文件目录,修改cmakelists target_link_libraries(paste_pic PRIVATE QHotkey) add_subdirectory(third_party/QHotkey)
- 通过QDialog QVBoxLayout QKeySequenceEdit收集快捷键输入
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| class SetHotkeyDialog : public QDialog { Q_OBJECT public: SetHotkeyDialog(QWidget* parent = nullptr) : QDialog(parent) { setWindowTitle("设置快捷键"); auto layout = new QVBoxLayout(this); keyEdit = new QKeySequenceEdit(this); auto btn = new QPushButton("确定", this); layout->addWidget(keyEdit); layout->addWidget(btn); connect(btn, &QPushButton::clicked, this, &QDialog::accept); } QKeySequence keySequence() const { return keyEdit->keySequence(); } private: QKeySequenceEdit* keyEdit; };
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| void SnipasteApp::setHotkey() { SetHotkeyDialog dlg; if (dlg.exec() == QDialog::Accepted) { QKeySequence seq = dlg.keySequence(); if (!seq.isEmpty()) { if (m_hotkey) { m_hotkey->setRegistered(false); delete m_hotkey; } m_hotkey = new QHotkey(seq, true, this); connect(m_hotkey, &QHotkey::activated, this, &SnipasteApp::grapScreen); } } }
|