查看完整版本: [-- 如何更改button的文字方向 --]

QTCN开发网 -> Qt基础编程 -> 如何更改button的文字方向 [打印本页] 登录 -> 注册 -> 回复主题 -> 发表主题

hellogritty 2010-03-08 16:49

如何更改button的文字方向


    请教各位达人,有没有什么函数能搞定
  
  或者用qss,可是只看到了text-align,没有看到text-direction

    QAbstractButton也没有rotate函数

  

benbenmajia 2010-03-08 16:55
呃,这样做好吗?
QString  ss  = "Hello\nWorld!";

benbenmajia 2010-03-08 16:56
\n很好很强大
PS:15秒内不能发帖。。

hellogritty 2010-03-08 16:57
引用第1楼benbenmajia于2010-03-08 16:55发表的  :
呃,这样做好吗?
QString  ss  = "HellonWorld!";


这样做只是把HelloWorld分为两行写,我想要的是文字竖排的效果

benbenmajia 2010-03-08 16:59
多加几个“\n”?
h\ne\n。。。。。。

benbenmajia 2010-03-08 17:00
也可以像小夜那么做,做一个位图,显示在button上~.

hellogritty 2010-03-08 20:47
引用第4楼benbenmajia于2010-03-08 16:59发表的  :
多加几个“n”?
hnen。。。。。。


我的button上显示的是英文,要每个字母都能转90度那样

benbenmajia 2010-03-08 21:36
做个位图吧

yb824 2010-03-08 22:18
试下~~~
.h
#include <QPushButton>

class MyButton : public QPushButton
{
public:
    MyButton(QWidget* parent=0);
protected:
    void paintEvent(QPaintEvent *event);
};
.cpp

MyButton::MyButton(QWidget *parent)
    :QPushButton(parent)
{

}

void MyButton::paintEvent(QPaintEvent *event)
{
    QString txt = this->text();
    setText("");
    QPushButton::paintEvent(event);
    setText(txt);
    QPainter p(this);

    p.rotate(90);
    p.translate(0, -width());
    QRect rect(this->rect().topLeft(),
               QSize(this->rect().height(), this->rect().width()));
    p.drawText(rect, Qt::AlignCenter, this->text() );
    p.end();

}

hellogritty 2010-03-09 09:35
引用第8楼yb824于2010-03-08 22:18发表的  :
试下~~~
.h
#include <QPushButton>
class MyButton : public QPushButton
.......


感谢yb824提供的代码,文字转过来了,可是按钮还是原来的方向,没有随文字一起转,
要想按钮也转,该怎么做呢?

yb824 2010-03-09 09:53
把高度设大一点,宽度设小一点,感觉就是竖着的了

hellogritty 2010-03-09 11:22
引用第10楼yb824于2010-03-09 09:53发表的  :
把高度设大一点,宽度设小一点,感觉就是竖着的了


回yb824,这段代码CPU占用很高啊,是setText(txt)引起的,把这行删了以后,txt又显示不出来(虽然有painter.drawText())。有没有什么好的解决方法呢?

hehui 2010-03-18 08:51
选用@开头的字体试试~

many_many 2010-05-04 14:49
引用第11楼hellogritty于2010-03-09 11:22发表的 :
回yb824,这段代码CPU占用很高啊,是setText(txt)引起的,把这行删了以后,txt又显示不出来(虽然有painter.drawText())。有没有什么好的解决方法呢?



那是因为你坐标没有计算正确!

15805201952 2021-03-23 08:24
yb824:试下~~~
.h
#include <QPushButton>
class MyButton : public QPushButton
....... (2010-03-08 22:18) 

这段代码是对QPushButton进行重定义吗,那我使用MyButton定义了一个按键,如何使用button->paintEvent();使按键上的文字旋转呀?
void MyButton::paintEvent(QPaintEvent *event)这个函数中的event不知道如何给。
我把代码改了下,但是按键上的字幕没有旋转。

//void MyButton::paintEvent(QPaintEvent *event)
void MyButton::paintEvent(void)
{
    QString txt = this->text();
    setText("");
//    QPushButton::paintEvent(event);
    setText(txt);
    QPainter p(this);
    p.rotate(90);
    p.translate(0, -width());
    QRect rect(this->rect().topLeft(), QSize(this->rect().height(), this->rect().width()));
    p.drawText(rect, Qt::AlignCenter, this->text() );
    p.end();

}

15805201952 2021-03-23 08:24
hellogritty:感谢yb824提供的代码,文字转过来了,可是按钮还是原来的方向,没有随文字一起转,
要想按钮也转,该怎么做呢? (2010-03-09 09:35) 

这段代码是对QPushButton进行重定义吗,那我使用MyButton定义了一个按键,如何使用button->paintEvent();使按键上的文字旋转呀?
void MyButton::paintEvent(QPaintEvent *event)这个函数中的event不知道如何给。
我把代码改了下,但是按键上的字幕没有旋转。

//void MyButton::paintEvent(QPaintEvent *event)
void MyButton::paintEvent(void)
{
    QString txt = this->text();
    setText("");
//    QPushButton::paintEvent(event);
    setText(txt);
    QPainter p(this);
    p.rotate(90);
    p.translate(0, -width());
    QRect rect(this->rect().topLeft(), QSize(this->rect().height(), this->rect().width()));
    p.drawText(rect, Qt::AlignCenter, this->text() );
    p.end();

}


查看完整版本: [-- 如何更改button的文字方向 --] [-- top --]



Powered by phpwind v8.7 Code ©2003-2011 phpwind
Gzip disabled