一、鼠标事件
1、点击鼠标左键时显示鼠标按下的坐标
void MainWindow::mousePressEvent(QMouseEvent *e)
{
if(e->button()==Qt::LeftButton)
{
qDebug()<<"press point:"<<e->x()<<" "<<e->y();
QPoint p(e->pos());
}
}
2、 ...
二、paintEvent
1.QPainter绘制点线
.h:
class MainWindow
{
//....
public:
void paintEvent(QPaintEvent *);
//...
}
:cpp:
void MainWindow::paintEvent(QPaintEvent *)
{
QPainter bgImage(this);
//bgImage.setRenderHint(QPainter::Antialiasing,true);
//Antialiasing:反锯齿
//如果为true,则设置给定的渲染提示;否则清除渲染提示
bgImage.setPen(QPen(Qt::black,5)); //设置pen方式1
painter.translate(10,10); //translate: painting画笔坐标偏移转换
QPoint qp(100,100);
bgImage.drawPoint(qp); //画点
painter.drawEllipse(QPoint(0,0),1,1); //绘制给定矩形定义的椭圆
qDebug()<<painter.brushOrigin()<<endl; //返回当前设置的画笔坐标QPoint
QPainter line_painter(this);
QPen line_pen;
line_pen = QPen(Qt::black,1,Qt::SolidLine); //设置pen SolidLine:默认线
line_pen.setStyle(Qt::CustomDashLine); //Custom:自定义 ;DotLine:点状线;
QVector<qreal> dashes;
qreal space = 2;
dashes << 1 << space << 3 << space; //1个点之后3个连续点,间隔设置未知
line_pen.setDashPattern(dashes);
line_painter.setPen(line_pen);
painter.drawLine(20,20,100,100); //画线
}
2....
一:截取整个屏幕
它就是QScreen类的grabWindow函数.
参数是窗口ID和要截取的区域(x、y、width和height组成的矩形区域)。
窗口ID可以通过QWidget的winId()获得,若截取整个屏幕窗口ID传入0。
如 QScreen *screen = QGuiApplication::primaryScreen(); // 这个是有界面的, 获取电脑的当前设置的主屏幕 还有类似函数 QApplication::primaryScreen();无ui界面的
QPixmap pximap = creen->grabWindow(0);
pximap.save(filePathName, "jpg");
如果你的应用程序是无界面的,直接使用QCoreApplication即可,如果是gui相关,但没有使用widgets模块的就使用QGuiApplication,否则使用QApplication。
继承关系:
QApplication <- QGuiApplication <- QCoreApplication <- QObject
二:截取某个控件(QWidget)
调用QWidget的grab方法
QPixmap p = this->grab(QRect(0, 0, 1000, 800));
p.save(filePathName,"png");
3、编译不同版本qt工程 'class QPainterPath' has no member named 'clear'
path.clear(); //换成 path = QPainterPath();
4、int转QString
int a = 1;
QString str = QString::nuber(a);
5、在固定的string中间指定位置添加内容
int a = 1;
QString str = QString(“%1bcd”).arg(a);
6、
"for(auto item:add)" 是一种 C++11 的语法,它表示使用 C++11 的新特性 "Range-based for loop",用于遍历容器中的所有元素。其中 "add" 是一个容器,"item" 是一个变量,每次循环都会将容器中的下一个元素赋值给该变量。