Qt实现QQ分组、联系⼈等页签下划线的效果
如上截图指的是切换消息、联系⼈、空间页签下⾯那两根线的效果或加粗现的效果,调⽤的地⽅这⾥不给出,只是说⼤致的实现思路,分为三部分:
声明:
#include <QtWidgets\QToolBar>
#include <QtWidgets\QMainWindow>
#include <QtWidgets\QLabel>
#include <QtGui\qevent.h>
#include <QtGui/qpainter.h>
#include <QtWidgets/QStylePainter>
#include <QSignalMapper>
qq分组简单
enum PageType
{
ptPageUnkonwn = -1, //表⽰未知
ptPageOne, //页签1
ptPageTwo, //页签2
ptPageThree //页签3
};
class DrawUnderLine :public QDialog
{
Q_OBJECT //信号与槽需要使⽤
public:
DrawUnderLine(QDialog *pParent = nullptr);
~DrawUnderLine();
void initUI();
protected:
void toolButtonSlotConnect(); //给每⼀个页签绑定信号槽
void paintEvent(QPaintEvent *e); //绘制事件⾥⾯进⾏线的绘制
void UpDateDrawLine(QStylePainter *p); //实现画线效果
protected slots: //槽函数
void GotoPage(int index); //切换页签时使⽤
private:
QToolButton *m_pPageOne;
QToolButton *m_pPageTwo;
QToolButton *m_pPageThree;
QToolBar *m_pLayoutTooblBar;
QLabel *m_SpaceLabel;
QSignalMapper *m_pSignalMapper;
QList<QToolButton*> m_btnList;
QList<PageType> m_btnMapIndex;
};
:第⼀部分初始界⾯
void DrawUnderLine::initUI()
{
QVBoxLayout* pVLayout = new QVBoxLayout();
m_pPageOne = new QToolButton(this);
m_pPageOne->setText("Page One");
m_pPageOne->setContentsMargins(0, 0, 0, 0);
m_pPageTwo = new QToolButton(this);
m_pPageTwo->setText("Page Two");
m_pPageTwo->setContentsMargins(0, 0, 0, 0);
m_pPageThree = new QToolButton(this);
m_pPageThree->setText("Page Three");
m_pPageOne->setFixedHeight(22);
m_pPageTwo->setFixedHeight(22);
m_pPageThree->setFixedHeight(22);
m_btnList.append(m_pPageOne);
m_btnList.append(m_pPageTwo);
m_btnList.append(m_pPageThree);
m_pPageOne->setContentsMargins(0, 0, 0, 0);
m_pPageTwo->setContentsMargins(0, 0, 0, 0);
m_pPageThree->setContentsMargins(0, 0, 0, 0);
m_btnMapIndex.append(ptPageOne);
m_btnMapIndex.append(ptPageTwo);
m_btnMapIndex.append(ptPageThree);
m_pLayoutTooblBar = new QToolBar();
m_SpaceLabel = new QLabel(this);
m_SpaceLabel->setFixedWidth(3);
m_SpaceLabel = new QLabel(this);
m_SpaceLabel->setFixedWidth(3);
m_pLayoutTooblBar->setContentsMargins(0, 0, 0, 0);
m_pLayoutTooblBar->addWidget(m_pPageOne);
m_pLayoutTooblBar->addWidget(m_SpaceLabel); //留点间隔 m_pLayoutTooblBar->addWidget(m_pPageTwo);
m_pLayoutTooblBar->addWidget(m_SpaceLabel); //留点间隔 m_pLayoutTooblBar->addWidget(m_pPageThree);
QHBoxLayout* pHLayoutTop = new QHBoxLayout;
pHLayoutTop->setContentsMargins(0, 0, 0, 535);
pHLayoutTop->addWidget(m_pLayoutTooblBar);
setLayout(pHLayoutTop);
setMinimumSize(400, 550);
resize(400, 550);
}
第⼆部分:信号与槽
void DrawUnderLine::toolButtonSlotConnect()
{
m_pSignalMapper = new QSignalMapper;
for (int i = 0; i < m_btnList.size(); ++i)
{
QToolButton* btn = m_btnList[i];
btn->setToolButtonStyle(Qt::ToolButtonTextOnly);
btn->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::MinimumExpanding); btn->setCheckable(true);
m_pSignalMapper->setMapping(btn, m_btnMapIndex[i]);
connect(btn, SIGNAL(clicked()), m_pSignalMapper, SLOT(map()));
}
connect(m_pSignalMapper, SIGNAL(mapped(int)), this, SLOT(GotoPage(int))); m_pPageOne->setChecked(true);
m_pPageTwo->setVisible(false);
m_pPageThree->setVisible(false);
}
void DrawUnderLine::GotoPage(int index) //实现切换页签时当前按钮的check状态
{
for (int i = 0; i < m_btnMapIndex.size(); ++i)
{
if (index == m_btnMapIndex[i])
{
m_btnList[i]->setChecked(true);
}
else
{
m_btnList[i]->setChecked(false);
}
}
}
第三部分:画线实现,核⼼实现效果
void DrawUnderLine::paintEvent(QPaintEvent *e)
{
QStylePainter p(this);
UpDateDrawLine(&p);
QDialog::paintEvent(e);
update();
}
void DrawUnderLine::UpDateDrawLine(QStylePainter *p)
{
p->save();
QColor colBottomBorder;
colBottomBorder.setNamedColor("#DC143C");
p->setPen(QPen(colBottomBorder, 1)); //画像素为1的长线
//画⼩的最长的分割线,65表⽰线在窗体中位置
p->drawLine(QPoint(rect().topLeft().x(), rect().topLeft().y() + 65), QPoint(rect().topRight().x(), rect().topRight().y() + 65)); p->restore();
p->save();
colBottomBorder.setNamedColor("#359eff");
p->setPen(QPen(colBottomBorder, 3)); //画⼀根像素为3的移动的短线
//画按钮下移动的线,粗那根线
if (m_pPageOne->isChecked()) //如果toolButton是被按下这画粗线
{
//计算toolButton的宽度,从左到右,依次累加定位
p->drawLine(QPoint(m_pPageOne->rect().topLeft().x() + 13, rect().topLeft().y() + 65),
QPoint(m_pPageOne->rect().topRight().x() + 17, rect().topRight().y() + 65));
}
else if (m_pPageTwo->isChecked())
{
p->drawLine(QPoint(m_pPageOne->rect().topRight().x() + 30, rect().topLeft().y() + 65),
QPoint(m_pPageOne->rect().topRight().x() + m_pPageTwo->rect().topRight().x() + 29, rect().topRight().y() + 65));
}
else if (m_pPageThree->isChecked())
{
p->drawLine(QPoint(m_pPageOne->rect().topRight().x() + m_pPageTwo->rect().topRight().x() + 45, rect().topLeft().y() + 65),
QPoint(m_pPageOne->rect().topRight().x() + m_pPageTwo->rect().topRight().x() + m_pPageThree-
>rect().topRight().x() + 43, rect().topRight().y() + 65));
}
p->restore();
}
构造和析构
DrawUnderLine::DrawUnderLine(QDialog *pParent) :QDialog(pParent)
{
initUI();
toolButtonSlotConnect();
}
DrawUnderLine::~DrawUnderLine()
{
if (m_pSignalMapper) //防⽌内存泄漏
{
delete m_pSignalMapper;
m_pSignalMapper = nullptr;
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论