| 查看: 517 | 回复: 3 | |||
peggycanon铜虫 (小有名气)
|
[求助]
Qt界面直线已有1人参与
|
|
跪求哪位大侠能帮哈忙,我想通过QT实现画线功能,要求如下: 点击第一下获取直线第一点,然后直线终点可以跟着鼠标移动,当点击第二下时,直线绘制完成! 就想autocad绘制直线一样 非常非常感谢谢!!! |
» 本帖已获得的红花(最新10朵)
» 猜你喜欢
三无产品还有机会吗
已经有5人回复
垃圾破二本职称评审标准
已经有5人回复
投稿返修后收到这样的回复,还有希望吗
已经有7人回复
压汞仪和BET测气凝胶孔隙率
已经有4人回复
博士申请都是内定的吗?
已经有14人回复
谈谈两天一夜的“延安行”
已经有13人回复
氨基封端PDMS和HDI反应快速固化
已经有11人回复
之前让一硕士生水了7个发明专利,现在这7个获批发明专利的维护费可从哪儿支出哈?
已经有11人回复
论文投稿求助
已经有4人回复
Applied Surface Science 这个期刊。有哪位虫友投过的能把word模板发给我参考一下嘛
已经有3人回复

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
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
- 专业: 信号理论与信号处理
【答案】应助回帖
|
这个也很简单,不过现在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;