Skip to content

Commit

Permalink
refactor: replace QFontMetrics.width with QFontMetrics.horizontalAdvance
Browse files Browse the repository at this point in the history
QFontMetrics.width已标记弃用
  • Loading branch information
myml committed Nov 16, 2024
1 parent 0e523ee commit da9540a
Show file tree
Hide file tree
Showing 17 changed files with 32 additions and 32 deletions.
4 changes: 2 additions & 2 deletions calendar-client/src/customWidget/cdateedit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ void CDateEdit::slotRefreshLineEditTextFormat(const QString &text)
{
QFont font = lineEdit()->font();
QFontMetrics fm(font);
int textWidth = fm.width(text);
int textWidth = fm.horizontalAdvance(text);
int maxWidth = lineEdit()->width() - 25; //文本能正常显示的最大宽度
if (textWidth > maxWidth) {
setToolTip(text);
Expand Down Expand Up @@ -224,7 +224,7 @@ bool CDateEdit::showGongli()
{
QString str = m_strCurrrentDate;
QFontMetrics fontMetrice(lineEdit()->font());
if (fontMetrice.width(str) > lineEdit()->width() - 20) {
if (fontMetrice.horizontalAdvance(str) > lineEdit()->width() - 20) {
return false;
}
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ void CColorPickerWidget::setLabelText() {
}
QString str = m_strColorLabel;
QFontMetrics fontMetrice(m_wordLabel->font());
if(fontMetrice.width(str) > (m_wordLabel->width()+4)) {
if(fontMetrice.horizontalAdvance(str) > (m_wordLabel->width()+4)) {
str = fontMetrice.elidedText(str,Qt::ElideRight,m_wordLabel->width());
}
m_wordLabel->setText(str);
Expand Down
4 changes: 2 additions & 2 deletions calendar-client/src/customWidget/ctitlewidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ void CTitleWidget::resizeEvent(QResizeEvent *event)
}
QString str = m_strPlaceHolder;
QFontMetrics fontMetrice(m_searchEdit->font());
if (fontMetrice.width(str) > (m_searchEdit->width() - 30) && m_clickShowLeft == false && !m_buttonBox->isHidden()) {
if (fontMetrice.horizontalAdvance(str) > (m_searchEdit->width() - 30) && m_clickShowLeft == false && !m_buttonBox->isHidden()) {
str = fontMetrice.elidedText(str, Qt::ElideRight, m_searchEdit->width() - 30);
m_searchEdit->setPlaceHolder(str);
m_searchEdit->setPlaceholderText(str);
Expand All @@ -263,7 +263,7 @@ void CTitleWidget::updateSearchEditPlaceHolder()
{
QString str = m_strPlaceHolder;
QFontMetrics fontMetrice(m_searchEdit->font());
if (fontMetrice.width(str) > (m_searchEdit->width() - 30)) {
if (fontMetrice.horizontalAdvance(str) > (m_searchEdit->width() - 30)) {
str = fontMetrice.elidedText(str, Qt::ElideRight, m_searchEdit->width() - 30);
m_searchEdit->setPlaceHolder(str);
m_searchEdit->setPlaceholderText(str);
Expand Down
4 changes: 2 additions & 2 deletions calendar-client/src/customWidget/customframe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ void CustomFrame::setTextStr(const QString &strc)

if (!m_fixsizeflag) {
QFontMetrics fm(m_font);
int w = fm.width(m_text);
int w = fm.horizontalAdvance(m_text);
setMinimumWidth(w);
}
update();
Expand All @@ -69,7 +69,7 @@ void CustomFrame::setTextFont(const QFont &font)

if (!m_fixsizeflag) {
QFontMetrics fm(m_font);
int w = fm.width(m_text);
int w = fm.horizontalAdvance(m_text);
setMinimumWidth(w);
}
}
Expand Down
2 changes: 1 addition & 1 deletion calendar-client/src/customWidget/jobtypelistview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ void JobTypeListViewStyle::paint(QPainter *painter, const QStyleOptionViewItem &
// 获取当前字体的信息
QFontMetrics fontMetrics(opt.font);
// 当前文字长度是否大于显示框长度
if (fontMetrics.width(displayName) > (opt.rect.width() - 90)) {
if (fontMetrics.horizontalAdvance(displayName) > (opt.rect.width() - 90)) {
displayName = fontMetrics.elidedText(displayName, Qt::ElideRight, opt.rect.width() - 90); // 截取字符串长度用...代替
}
if (view && view->m_iIndexCurrentHover == index.row()) {
Expand Down
6 changes: 3 additions & 3 deletions calendar-client/src/customWidget/scheduleRemindWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ void CenterWidget::UpdateTextList()
{
testList.clear();
QFontMetrics metrics(textfont);
textwidth = metrics.width(m_ScheduleInfo->summary());
textwidth = metrics.horizontalAdvance(m_ScheduleInfo->summary());
textheight = metrics.height();
const int h_count = qCeil(textwidth / textRectWidth);
QString text;
Expand All @@ -131,7 +131,7 @@ void CenterWidget::UpdateTextList()

for (int i = 0; i < m_ScheduleInfo->summary().count(); ++i) {
text += m_ScheduleInfo->summary().at(i);
if (metrics.width(text) > textRectWidth) {
if (metrics.horizontalAdvance(text) > textRectWidth) {
text.remove(text.count() - 1, 1);
testList.append(text);
text = "";
Expand Down Expand Up @@ -172,7 +172,7 @@ void CenterWidget::paintEvent(QPaintEvent *e)
QFontMetrics metrics(timeFont);
if (m_ScheduleInfo->allDay())
timestr = tr("All Day");
int timewidth = metrics.width(timestr);
int timewidth = metrics.horizontalAdvance(timestr);
int timeheight = metrics.height();

painter.drawText(QRect(x + 13, 7, timewidth, timeheight), Qt::AlignLeft | Qt::AlignTop, timestr);
Expand Down
2 changes: 1 addition & 1 deletion calendar-client/src/customWidget/timeedit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ void CTimeEdit::showPopup()
int maxLen = 0;
QFontMetrics fontMet(view()->font());
for (int i = 0 ; i < count() ; ++i) {
int &&itemWidth = fontMet.width(this->itemText(i));
int &&itemWidth = fontMet.horizontalAdvance(this->itemText(i));
maxLen = qMax(maxLen, itemWidth);
}
maxLen += 45; //选项前√占用的大小
Expand Down
6 changes: 3 additions & 3 deletions calendar-client/src/dialog/myscheduleview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ void CMyScheduleView::slotAutoFeed(const QFont &font)
labelF.setWeight(QFont::Medium);
labelF = DFontSizeManager::instance()->get(DFontSizeManager::T6, labelF);
QFontMetrics fm(labelF);
int titlewidth = fm.width(strText);
int titlewidth = fm.horizontalAdvance(strText);
QStringList strList;
QString str;
int h = fm.height();
Expand All @@ -77,7 +77,7 @@ void CMyScheduleView::slotAutoFeed(const QFont &font)
for (int i = 0; i < strText.count(); i++) {
str += strText.at(i);

if (fm.width(str) > 330) {
if (fm.horizontalAdvance(str) > 330) {
str.remove(str.count() - 1, 1);
strList.append(str);
resultStr += str + "\n";
Expand Down Expand Up @@ -108,7 +108,7 @@ void CMyScheduleView::slotAutoFeed(const QFont &font)
if (index != -1) {
timeName[index - 1] = ' ';
QFontMetrics fm(m_timeLabel->font());
int textWidth = fm.width(m_timeLabel->text());
int textWidth = fm.horizontalAdvance(m_timeLabel->text());
if (textWidth > m_timeLabel->width()) {
timeName[index - 1] = '\n';
m_timeLabelH = 58;
Expand Down
4 changes: 2 additions & 2 deletions calendar-client/src/dialog/schedulectrldlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ void CScheduleCtrlDlg::changeEvent(QEvent *event)
QFontMetrics font_button(font);
QFontMetrics font_firstLabel(font);
QFontMetrics font_seconLabel(font);
int height_firstLabel = (font_firstLabel.width(m_firstLabel->text()) / 300 + 1) * font_firstLabel.height();
int height_seconLabel = (font_seconLabel.width(m_seconLabel->text()) / 300 + 1) * font_seconLabel.height();
int height_firstLabel = (font_firstLabel.horizontalAdvance(m_firstLabel->text()) / 300 + 1) * font_firstLabel.height();
int height_seconLabel = (font_seconLabel.horizontalAdvance(m_seconLabel->text()) / 300 + 1) * font_seconLabel.height();

for (int i = 0; i < buttonCount(); i++) {
QAbstractButton *button = getButton(i);
Expand Down
6 changes: 3 additions & 3 deletions calendar-client/src/view/graphicsItem/calldayscheduleitem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,15 @@ void CAllDayScheduleItem::paintBackground(QPainter *painter, const QRectF &rect,
tSTitleName.replace("\n", "");
QString str = tSTitleName;
QString tStr;
int _rightOffset = fm.width("...");
int _rightOffset = fm.horizontalAdvance("...");
//显示宽度 左侧偏移13右侧偏移8
qreal _showWidth = fillRect.width() - 13 - 8 - m_offset * 2;
//如果标题总长度大于显示长度则显示长度须减去"..."的长度
if (fm.width(str) > _showWidth) {
if (fm.horizontalAdvance(str) > _showWidth) {
_showWidth -= _rightOffset;
for (int i = 0; i < str.count(); i++) {
tStr.append(str.at(i));
int widthT = fm.width(tStr);
int widthT = fm.horizontalAdvance(tStr);
//如果宽度大于显示长度则去除最后添加的字符
if (widthT > _showWidth) {
tStr.chop(1);
Expand Down
2 changes: 1 addition & 1 deletion calendar-client/src/view/graphicsItem/cmonthdayitem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ void CMonthDayItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *opt
//绘制农历
if (m_LunarVisible) {
QFontMetrics metrics(m_LunerFont);
int Lunarwidth = metrics.width(m_DayLunar);
int Lunarwidth = metrics.horizontalAdvance(m_DayLunar);
qreal filleRectX = this->rect().width() - 12 - 3 - (58 + Lunarwidth) / 2;
QRectF fillRectT(this->rect().x() + filleRectX,
this->rect().y() + 9,
Expand Down
6 changes: 3 additions & 3 deletions calendar-client/src/view/graphicsItem/cmonthscheduleitem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,15 @@ void CMonthScheduleItem::paintBackground(QPainter *painter, const QRectF &rect,
//右侧偏移8
qreal textWidth = labelwidth - m_pos.x() - m_offset * 2 - 8;
QString tStr;
int _rightOffset = fm.width("...");
int _rightOffset = fm.horizontalAdvance("...");
//显示宽度 左侧偏移13右侧偏移8
qreal _showWidth = textWidth;
//如果标题总长度大于显示长度则显示长度须减去"..."的长度
if (fm.width(str) > _showWidth) {
if (fm.horizontalAdvance(str) > _showWidth) {
_showWidth -= _rightOffset;
for (int i = 0; i < str.count(); i++) {
tStr.append(str.at(i));
int widthT = fm.width(tStr);
int widthT = fm.horizontalAdvance(tStr);
//如果宽度大于显示长度则去除最后添加的字符
if (widthT > _showWidth) {
tStr.chop(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ void CMonthScheduleNumItem::paint(QPainter *painter, const QStyleOptionGraphicsI
QString tStr;
for (int i = 0; i < str.count(); i++) {
tStr.append(str.at(i));
int widthT = fm.width(tStr) + 5;
int widthT = fm.horizontalAdvance(tStr) + 5;
if (widthT >= labelwidth) {
tStr.chop(2);
break;
Expand Down
8 changes: 4 additions & 4 deletions calendar-client/src/view/graphicsItem/scheduleitem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ void CScheduleItem::splitText(QFont font, int w, int h, QString str, QStringList

for (int i = 0; i < str.count(); i++) {
tStr.append(str.at(i));
int widthT = fontMetrics.width(tStr) + 5;
int widthT = fontMetrics.horizontalAdvance(tStr) + 5;

if (widthT >= w) {
tStr.chop(1);
Expand All @@ -107,7 +107,7 @@ void CScheduleItem::splitText(QFont font, int w, int h, QString str, QStringList
QString c = str.at(0);
QString str = c + "...";
QFontMetrics fm(font);
while (f_st.width(str) > w && f_st.width(str) > 24) {
while (f_st.horizontalAdvance(str) > w && f_st.horizontalAdvance(str) > 24) {
str.chop(1);
}
listStr.append(str);
Expand Down Expand Up @@ -255,11 +255,11 @@ void CScheduleItem::paintBackground(QPainter *painter, const QRectF &rect, const
QFontMetrics fontMetrics(font);
qreal drawTextWidth = rect.width() - m_offset * 2;

if (fm.width(str) > drawTextWidth - 5) {
if (fm.horizontalAdvance(str) > drawTextWidth - 5) {
QString tStr;
for (int i = 0; i < str.count(); i++) {
tStr.append(str.at(i));
int widthT = fm.width(tStr) - 5;
int widthT = fm.horizontalAdvance(tStr) - 5;

if (widthT >= drawTextWidth) {
if (i < 1) {
Expand Down
2 changes: 1 addition & 1 deletion calendar-client/src/widget/schedulesearchview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ void CScheduleSearchItem::paintEvent(QPaintEvent *e)
for (int i = 0; i < str.count(); i++) {
tStr.append(str.at(i));
tStr.append(ellipsis);
int widthT = fm.width(tStr);
int widthT = fm.horizontalAdvance(tStr);
tStr.remove(ellipsis);
if (widthT >= tilenameW) {
tStr.chop(1);
Expand Down
2 changes: 1 addition & 1 deletion calendar-client/src/widget/weekWidget/weekheadview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ void CWeekHeadView::paintCell(QWidget *cell)

QFontMetrics fm = painter.fontMetrics();

while (fm.width(dayWeek) > cell->width() / 2)
while (fm.horizontalAdvance(dayWeek) > cell->width() / 2)
dayWeek.chop(1);
//水平右对齐,上下居中
painter.drawText(QRect(0, bh, (cell->width() / 2), 26), Qt::AlignRight | Qt::AlignVCenter, dayWeek);
Expand Down
2 changes: 1 addition & 1 deletion calendar-client/src/widget/yearWidget/yearscheduleview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ void CYearScheduleView::paintItem(QPainter &painter, DSchedule::Ptr info, int in

for (int i = 0; i < str.count(); i++) {
tStr.append(str.at(i));
int widthT = fm.width(tStr) + 5;
int widthT = fm.horizontalAdvance(tStr) + 5;
if (widthT >= tilenameW) {
tStr.chop(1);
break;
Expand Down

0 comments on commit da9540a

Please sign in to comment.