| 查看: 591 | 回复: 3 | ||
| 当前只显示满足指定条件的回帖,点击这里查看本话题的所有回帖 | ||
peggycanon铜虫 (小有名气)
|
[求助]
Qt界面直线 已有1人参与
|
|
|
跪求哪位大侠能帮哈忙,我想通过QT实现画线功能,要求如下: 点击第一下获取直线第一点,然后直线终点可以跟着鼠标移动,当点击第二下时,直线绘制完成! 就想autocad绘制直线一样 非常非常感谢谢!!! |
» 本帖已获得的红花(最新10朵)
» 猜你喜欢
实名举报:华南理工大学物理与光电学院副院长李志远婚内两次出轨女学生(博士)
已经有10人回复
现代”学阀”该如何界定
已经有7人回复
各位大神,目前国内有哪些比较好用的逆合成软件?
已经有12人回复
我的奶奶
已经有3人回复
申博发邮件
已经有10人回复
国社科系统bug了,是不是要放榜了?
已经有9人回复
上海工程技术大学激光智能制造课题组|2027级博士研究生招生公告
已经有8人回复
上海工程技术大学激光智能制造课题组招收博士研究生
已经有8人回复

peggycanon
铜虫 (小有名气)
- 应助: 0 (幼儿园)
- 金币: 110.5
- 红花: 1
- 帖子: 60
- 在线: 12.3小时
- 虫号: 1054470
- 注册: 2010-07-09
- 性别: GG
- 专业: 计算机软件

3楼2014-08-19 08:07:55
fighter0593
金虫 (小有名气)
- 应助: 20 (小学生)
- 金币: 608.7
- 散金: 120
- 红花: 10
- 帖子: 181
- 在线: 90.9小时
- 虫号: 2666401
- 注册: 2013-09-19
- 性别: GG
- 专业: 信号理论与信号处理
【答案】应助回帖
感谢参与,应助指数 +1
|
我想了一下,给你一个思路看看吧. 在UI界面里面拖一个Graphics View,然后在上面绘图,但是貌似里面没有鼠标事件,所以无法获得坐标,需要自己继承Graphics View再写一个鼠标事件来进行绘图. 我写了一个单画直线的例子,你参考一下吧. //----------dialog.h文件------------------ #ifndef DIALOG_H #define DIALOG_H #include <QDialog> #include <QtCore> #include <QtGui> #include <QGraphicsScene> namespace Ui { class Dialog; } class Dialog : public QDialog { Q_OBJECT public: explicit Dialog(QWidget *parent = 0); ~Dialog(); private: Ui: ialog *ui;QGraphicsScene *scene; QGraphicsLineItem *line; }; #endif // DIALOG_H //----------dialog.cpp文件------------------ #include "dialog.h" #include "ui_dialog.h" Dialog: ialog(QWidget *parent) :QDialog(parent), ui(new Ui: ialog){ ui->setupUi(this); scene=new QGraphicsScene(this); ui->graphicsView->setScene (scene); QPen blackpen(Qt::black); blackpen.setWidth (6); line=scene->addLine (0,0,100,100,blackpen); } Dialog::~Dialog() { delete ui; } //----------main.cpp文件------------------ #include "dialog.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); Dialog w; w.show(); return a.exec(); } 另外那个GUI文件就是直接拖了一个Graphics View而已 |
2楼2014-08-19 02:15:34
fighter0593
金虫 (小有名气)
- 应助: 20 (小学生)
- 金币: 608.7
- 散金: 120
- 红花: 10
- 帖子: 181
- 在线: 90.9小时
- 虫号: 2666401
- 注册: 2013-09-19
- 性别: GG
- 专业: 信号理论与信号处理
【答案】应助回帖
|
这个也很简单,不过现在QPainter只能在paintEvent里面实现,需要更新重新绘图的时候直接调用update就可以了.我写了个小程序,左键单击记录起始点坐标,然后右键单击直接画一条直线连接这两个点. 代码如下,你参考一下吧 //------dialog.h----------- #ifndef DIALOG_H #define DIALOG_H #include <QDialog> #include <QPoint> #include <QWidget> #include <QMouseEvent> namespace Ui { class Dialog; } class Dialog : public QDialog { Q_OBJECT public: explicit Dialog(QWidget *parent = 0); ~Dialog(); QPoint firstpoint; QPoint lastpoint; protected: void mousePressEvent(QMouseEvent *event); void paintEvent(QPaintEvent *event); private: Ui: ialog *ui;void drawLineTo(const QPoint &endPoint); }; #endif // DIALOG_H //------------dialog.cpp---------- #include "dialog.h" #include "ui_dialog.h" #include <QDebug> #include <QPainter> #include <QLineF> Dialog: ialog(QWidget *parent) :QDialog(parent), ui(new Ui: ialog){ ui->setupUi(this); } void Dialog::mousePressEvent (QMouseEvent *event) { if (event->button () ==Qt::LeftButton) { this->firstpoint = event->pos(); } else if (event->button () ==Qt::RightButton) { this->lastpoint = event->pos(); update (); } } void Dialog::paintEvent (QPaintEvent *event) { QPainter painter(this); painter.drawLine(firstpoint.x (), firstpoint.y (), lastpoint.x (),lastpoint.y ()); } Dialog::~Dialog() { delete ui; } //--------------main.cpp-------------- #include "dialog.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); Dialog w; w.show(); return a.exec(); } |
4楼2014-08-19 14:15:11










回复此楼
peggycanon
ialog *ui;