用鼠标改变QWidget的宽度
Changing width of QWidget with mouse
我在 QHBoxLayout 中有两个 QWidget。我希望能够通过单击右侧并移动鼠标(例如 Qt Editor 的项目浏览器)来更改左侧 QWidget 的宽度。
您可以使用 eventFilter
并在小部件上使用鼠标移动、输入、离开和鼠标单击。
检查这个例子:
我用 QHBoxLayout
制作了两个小部件,并得到了 QEvent::HoverEnter
和 QEvent::MouseButtonPress
。
.cpp 文件:
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
widgetOneW = 300;
widgetTwoW = 300;
ui->widgetOne->installEventFilter(this);
ui->widgetTwo->installEventFilter(this);
ui->widgetOne->setAttribute(Qt::WA_Hover);
ui->widgetTwo->setAttribute(Qt::WA_Hover);
}
bool MainWindow::eventFilter(QObject *object, QEvent *event)
{
if ((object == ui->widgetOne) && (event->type() == QEvent::HoverEnter))
{
ui->widgetOne->resize(100, ui->widgetOne->height());
ui->widgetTwo->resize(widgetTwoW, ui->widgetTwo->height());
return true;
}
else if ((object == ui->widgetOne) && (event->type() == QEvent::MouseButtonPress))
{
QMouseEvent *keyEvent = static_cast<QMouseEvent *> (event);
if(keyEvent->button() == Qt::LeftButton)
{
ui->widgetOne->resize(100, ui->widgetOne->height());
ui->widgetTwo->resize(widgetTwoW, ui->widgetTwo->height());
return true;
}
}
else if ((object == ui->widgetTwo) && (event->type() == QEvent::HoverEnter))
{
ui->widgetOne->resize(widgetOneW, ui->widgetOne->height());
ui->widgetTwo->resize(100, ui->widgetTwo->height());
return true;
}
else if ((object == ui->widgetTwo) && (event->type() == QEvent::MouseButtonPress))
{
QMouseEvent *keyEvent = static_cast<QMouseEvent *> (event);
if(keyEvent->button() == Qt::LeftButton)
{
ui->widgetOne->resize(widgetOneW, ui->widgetOne->height());
ui->widgetTwo->resize(100, ui->widgetTwo->height());
}
}
}
MainWindow::~MainWindow()
{
delete ui;
}
.h 文件:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QKeyEvent>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
int widgetOneW;
int widgetTwoW;
public slots:
bool eventFilter(QObject *object, QEvent *event);
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
.pro 文件:
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = first
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
你可以使用 QSplitter 做你想做的事。你可以在这里找到一个完整的例子:
我在 QHBoxLayout 中有两个 QWidget。我希望能够通过单击右侧并移动鼠标(例如 Qt Editor 的项目浏览器)来更改左侧 QWidget 的宽度。
您可以使用 eventFilter
并在小部件上使用鼠标移动、输入、离开和鼠标单击。
检查这个例子:
我用 QHBoxLayout
制作了两个小部件,并得到了 QEvent::HoverEnter
和 QEvent::MouseButtonPress
。
.cpp 文件:
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
widgetOneW = 300;
widgetTwoW = 300;
ui->widgetOne->installEventFilter(this);
ui->widgetTwo->installEventFilter(this);
ui->widgetOne->setAttribute(Qt::WA_Hover);
ui->widgetTwo->setAttribute(Qt::WA_Hover);
}
bool MainWindow::eventFilter(QObject *object, QEvent *event)
{
if ((object == ui->widgetOne) && (event->type() == QEvent::HoverEnter))
{
ui->widgetOne->resize(100, ui->widgetOne->height());
ui->widgetTwo->resize(widgetTwoW, ui->widgetTwo->height());
return true;
}
else if ((object == ui->widgetOne) && (event->type() == QEvent::MouseButtonPress))
{
QMouseEvent *keyEvent = static_cast<QMouseEvent *> (event);
if(keyEvent->button() == Qt::LeftButton)
{
ui->widgetOne->resize(100, ui->widgetOne->height());
ui->widgetTwo->resize(widgetTwoW, ui->widgetTwo->height());
return true;
}
}
else if ((object == ui->widgetTwo) && (event->type() == QEvent::HoverEnter))
{
ui->widgetOne->resize(widgetOneW, ui->widgetOne->height());
ui->widgetTwo->resize(100, ui->widgetTwo->height());
return true;
}
else if ((object == ui->widgetTwo) && (event->type() == QEvent::MouseButtonPress))
{
QMouseEvent *keyEvent = static_cast<QMouseEvent *> (event);
if(keyEvent->button() == Qt::LeftButton)
{
ui->widgetOne->resize(widgetOneW, ui->widgetOne->height());
ui->widgetTwo->resize(100, ui->widgetTwo->height());
}
}
}
MainWindow::~MainWindow()
{
delete ui;
}
.h 文件:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QKeyEvent>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
int widgetOneW;
int widgetTwoW;
public slots:
bool eventFilter(QObject *object, QEvent *event);
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
.pro 文件:
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = first
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
你可以使用 QSplitter 做你想做的事。你可以在这里找到一个完整的例子: