自己在设置按钮样式的时候,想要实现字体在左边,图标在右边,网上找到的比较简单的方法是直接调用QPushButton::setLayoutDirection(Qt::LayoutDirection direction);
例如:
pushbutton1->setLayoutDirection(Qt::RightToLeft);
但是自己在使用这种方法的时候发现图还在但是字不见了
![](https://img-blog.csdnimg.cn/bb2cb99505014811a412445145f5189f.jpeg)
![设置](https://img-blog.csdnimg.cn/f5e268f24b5942f6a5d7015f7db9e5a8.png)
可以通过设置样式表来改变
setStyleSheet("QPushButton{spacing:10px;}")
#include "mainwindow.h"
#include <QPushButton>
#include <QApplication>
#include <QStyle>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
this->setGeometry(0,0,800,480);
QPushButton *btn = new QPushButton(this);
btn->setGeometry(200,200,200,50);
btn->setText("这是一个按钮");
//使用Qt自带的图标,需要<QApplication>和<QStyle>
btn->setIcon(QApplication::style()->standardIcon(QStyle::SP_ToolBarHorizontalExtensionButton));
btn->setLayoutDirection(Qt::RightToLeft);
//spacing设置多少都没有影响,文字都是靠左
btn->setStyleSheet("QPushButton{spacing : 0px;}");
}
MainWindow::~MainWindow()
{
}
参考连接: