簡介
使用QT制作一個UI圖片切換框架。
思路:主要通過移動像素坐標差值來判斷方向,左上角坐標為(0,0),右加左減,松開減去按壓時的橫坐標大于0則右移,否則左移。
代碼展示
#define X_Threshold_Direction 4 //X方向移動量
#define Y_Threshold_Direction 40 //Y方向移動量
添加點擊事件
this->installEventFilter(this); //設置點擊事件
點擊事件實現文章來源:http://www.zghlxwxcb.cn/news/detail-561178.html
bool MainWindow::eventFilter(QObject *watch, QEvent *evn)
{
static int press_x_value; //點擊時屏幕的橫坐標
static int press_y_value; //點擊時屏幕的縱坐標
static int relea_x_value; //松開時屏幕的橫坐標
static int relea_y_value; //松開時屏幕的縱坐標
QMouseEvent *event = static_cast<QMouseEvent *>(evn);
//獲取點擊鼠標(手指)時的坐標
if (event->type() == QEvent::MouseButtonPress)
{
press_x_value = event->globalX();
press_y_value = event->globalY();
}
//獲取松開鼠標(手指)時的坐標
if(event->type() == QEvent::MouseButtonRelease)
{
relea_x_value = event->globalX();
relea_y_value = event->globalY();
}
//對鼠標(手指)滑動的方向進行判斷(右滑)
if((relea_x_value - press_x_value) > X_Threshold_Direction && event->type() == QEvent::MouseButtonRelease && qAbs(relea_y_value - press_y_value) < Y_Threshold_Direction)
{
if (index == 4)
{
index = 1;
}
else
{
index++;
}
this->setStyleSheet(QString("border-image: url(:/image/%1.jpg);").arg(index)); //切換圖片
qDebug() << "right move";
}
//對鼠標(手指)滑動的方向進行判斷(左滑)
if((press_x_value - relea_x_value) > X_Threshold_Direction && event->type() == QEvent::MouseButtonRelease && qAbs(relea_y_value - press_y_value) < Y_Threshold_Direction)
{
if(index == 1)
{
index = 4;
}
else
{
index--;
}
this->setStyleSheet(QString("border-image: url(:/image/%1.jpg);").arg(index)); //切換圖片
qDebug() << "left move";
}
return QWidget::eventFilter(watch, evn);
}
效果如下
video文章來源地址http://www.zghlxwxcb.cn/news/detail-561178.html
到了這里,關于QT之滑動切換UI框架的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!