如何使QTextEdit背景透明

      前边的文章http://blog.csdn.net/NRC_DouNingBo/archive/2010/04/28/5540427.aspx介绍了如何给QTextEdit加背景图片,但是由于图片也会跟着翻屏,所以用了一种折中的办法就是:将QTextEdit设置为透明,而后在其后边的窗口的该位置处刷上图片即可。  那这里就说一下如何让QTextEdit控件背景变为透明。

 

     正如上一篇文章所讲的那样,在Qt中所有问题都要分两种体系来讨论,一种是QWidget体系,一种则是QGraphicsWidget体系。此处也不例外。

 

   一: 对于QWidget体系而言:亦即用的都是QWidget以及其派生类。对于此处:亦即QTextEdit的父对象也是QWidget或其派生类。

那么我们要做的就是:让QTextEdit背景变为透明,而后在其后边的父窗口该位置处刷上图片。

 

    ①设置QTextEdit为背景透明:

        QPalette pl = iEdit->palette();

        pl.setBrush(QPalette::Base,QBrush(QColor(255,0,0,0)));

        iEdit->setPalette(pl);

 

        即:用完全透明的画刷来刷一下编辑框的背景即可!

 

        而其父窗口在该位置处刷图片,关键是注意这个父窗口是不是顶层窗口(其有无父窗口),若是则注意不能使用setStyleSheet()来刷(原因见文章http://blog.csdn.net/NRC_DouNingBo/archive/2010/05/07/5565212.aspx)。

 

 

   二:对于QGraphicsWidget体系,则用如下方式来设置,这里我直接用代码了:

 

     MainWindow::MainWindow(QWidget *parent)
    : QGraphicsView(parent)
   {
      this->resize(360,640);
      iScene = new QGraphicsScene(0,0,360,640);

      iEdit = new QTextEdit();
      iEdit->resize(360,400);

 

     //下边这段给父view刷图片
     QPalette palette;
     palette.setBrush(this->backgroundRole(),QBrush(QImage(":/bmp/dou.jpg")));
     this->setPalette(palette);

 

     //此段负责设置该编辑框item背景透明

     palette.setBrush(QPalette::Base,QBrush(QColor(255,0,0,0)));
     iEdit->setPalette(palette);

     QGraphicsProxyWidget* widget = iScene->addWidget(iEdit);

     palette.setBrush(QPalette::Window,QBrush(QColor(255,0,0,0)));
     widget->setPalette(palette);


     this->setScene(iScene);
}

 

      可见,这其中还是有很大区别的,而且似乎有些地方难以理解,其实关键这里边牵扯两个问题,一个是QWidget体系和QGraphicsWidget体系的区别是什么?  一个是用样式表QPalette设置背景颜色/图片的两种方法的区别(亦即:QPalette的setBrush()函数的第一个参数是QPalette::Base形式 还是 ptr->backgrounRole() 的区别)。

 

      关于这两个问题, 我过会会写两外两篇文章来专门论述一下,但是上边代码你可以直接拷贝拿去用,没问题的。

 

      测试环境:Qt4.5.0 + VC2005

 

后补:

1:QWidget体系与QGraphicsWidget体系之间的区别

http://blog.csdn.net/NRC_DouNingBo/archive/2010/05/09/5571149.aspx

 

2:Qt中如何使用样式表QPalette以及相关注意事项

http://blog.csdn.net/NRC_DouNingBo/archive/2010/05/09/5571187.aspx

### 回答1: 您可以使用QTextEdit的`setProperty`方法来设置自定义属性,然后在QSS样式表中使用`QTextEdit[propertyName="propertyValue"]`来设置当该属性为特定值时QTextEdit的样式。 例如: ``` QTextEdit *textEdit = new QTextEdit; textEdit->setProperty("customProperty", "value"); textEdit->setStyleSheet("QTextEdit[customProperty=\"value\"] { border: none; background-color: transparent; }"); ``` 这样,当`customProperty`属性的值为"value"时,QTextEdit的边框和背景色都会被去掉。 注意,在使用QSS设置样式时,属性值需要用双引号括起来。 ### 回答2: 要根据QTextEdit某个自定义属性的变化,使用QSS来去掉QTextEdit的边框和背景色,可以按照以下步骤进行操作: 1. 创建一个继承自QTextEdit的自定义类,例如CustomTextEdit。 2. 在CustomTextEdit类中添加一个setter方法,用于设置自定义属性的值,并在此方法中根据属性的变化来更新QSS样式。例如,当自定义属性值为true时,使用QSS设置边框和背景色为透明;当自定义属性值为false时,使用QSS设置边框和背景色为默认值。 3. 在使用CustomTextEdit的地方,通过调用setCustomProperty方法来设置自定义属性的值。 下面是一个示例如何实现上述步骤: ```cpp // customtextedit.h 文件 #include <QTextEdit> class CustomTextEdit : public QTextEdit { Q_OBJECT public: explicit CustomTextEdit(QWidget *parent = nullptr); void setCustomProperty(bool customProperty); private: bool m_customProperty; }; ``` ```cpp // customtextedit.cpp 文件 #include "customtextedit.h" CustomTextEdit::CustomTextEdit(QWidget *parent) : QTextEdit(parent) { m_customProperty = false; // 默认属性值为false } void CustomTextEdit::setCustomProperty(bool customProperty) { m_customProperty = customProperty; // 根据属性值更新QSS样式 if (m_customProperty) { QString qss = "QTextEdit { background-color: transparent; border: none; }"; setStyleSheet(qss); } else { setStyleSheet(""); } } ``` 使用CustomTextEdit的示例: ```cpp CustomTextEdit *textEdit = new CustomTextEdit(this); textEdit->setCustomProperty(true); // 设置自定义属性为true textEdit->setPlainText("这是一个带有自定义属性的QTextEdit"); ``` 上述示例中,当设置自定义属性为true时,会根据QSS样式将CustomTextEdit的边框和背景色设置为透明。当设置自定义属性为false时,会将QSS样式设置为空,恢复默认的边框和背景色。 ### 回答3: 可以通过以下步骤来根据QTextEdit的自定义属性的变化使用QSS来去掉边框和背景色: 1. 在QTextEdit的子类中定义一个自定义属性,例如"customAttribute"。 ```cpp class MyTextEdit : public QTextEdit { Q_OBJECT public: explicit MyTextEdit(QWidget *parent = nullptr); // 定义自定义属性 void setCustomAttribute(bool value); bool getCustomAttribute(); private: bool m_customAttribute; }; ``` 2. 在实现文件中对自定义属性进行设置和获取。 ```cpp MyTextEdit::MyTextEdit(QWidget *parent) : QTextEdit(parent) { m_customAttribute = false; } void MyTextEdit::setCustomAttribute(bool value) { m_customAttribute = value; } bool MyTextEdit::getCustomAttribute() { return m_customAttribute; } ``` 3. 在需要监听属性变化的地方,比如在对话框的构造函数中,使用QObject::startTimer()来监听自定义属性的变化。 ```cpp MyTextEdit *myTextEdit = new MyTextEdit(this); myTextEdit->setObjectName("myTextEdit"); myTextEdit->setCustomAttribute(false); QObject::startTimer(100, Qt::TimerType::CoarseTimer); // 启动定时器 ``` 4. 在定时器事件中获取自定义属性的变化,如果变为true,则通过QSS来去掉QTextEdit的边框和背景色。 ```cpp void MyDialog::timerEvent(QTimerEvent *event) { if (event->timerId() == this->timerId()) { MyTextEdit *myTextEdit = this->findChild<MyTextEdit *>("myTextEdit"); // 获取自定义属性的变化 bool customAttribute = myTextEdit->getCustomAttribute(); // 根据属性变化,设置QSS if (customAttribute) { myTextEdit->setStyleSheet("border: none; background-color: transparent;"); } else { myTextEdit->setStyleSheet(""); } } } ``` 通过以上步骤,可以根据QTextEdit的自定义属性的变化使用QSS来去掉QTextEdit的边框和背景色。
评论 23
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值