简体   繁体   中英

How to specify style sheets for my classes in QT

I have a class that inherits from qframe, the "darksheetstyle" qss file is:

    /* (dot) .QFrame  fix #141, #126, #123 */
.QFrame {
  border-radius: 4px;
  border: 1px solid #455364;
  /* No frame */
  /* HLine */
  /* HLine */
}

.QFrame[frameShape="0"] {
  border-radius: 4px;
  border: 1px transparent #455364;
}

.QFrame[frameShape="4"] {
  max-height: 2px;
  border: none;
  background-color: #455364;
}

.QFrame[frameShape="5"] {
  max-width: 2px;
  border: none;
  background-color: #455364;
}

the QFrame has no border, so I wrote my class qss in the same qss file:

BorderFrame {
  border-radius: 4px;
  border: 2px solid #455364;
  /* No frame */
  /* HLine */
  /* HLine */
}

the cpp code is:

class ULTRA_PLOT_EXPORT BorderedFrame : public QFrame
    {
        Q_OBJECT
    public:
        explicit BorderedFrame(QWidget* parent) : QFrame(parent)
        {
            setParent(parent);
        }
        ~BorderedFrame() override = default;
        BorderedFrame(const BorderedFrame&) = delete;
        BorderedFrame& operator=(const BorderedFrame&) = delete;
        BorderedFrame(BorderedFrame&&) = delete;
        BorderedFrame& operator=(BorderedFrame&&) = delete;
        void paintEvent(QPaintEvent* event) override
        {
            QStyleOption opt;
            opt.initFrom(this);
            QPainter p(this);
            style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);//绘制样式
        }
    };

then, My own QFrame class still has no borders, how to fixed it?

This works as expected for me:

#include <QtWidgets>

class BorderedFrame : public QFrame
{
  Q_OBJECT
public:
  using QFrame::QFrame;
};

int main(int argc, char *argv[])
{
  QApplication app(argc, argv);

  app.setStyleSheet("BorderedFrame { border-radius: 4px; border: 2px solid #ff0000; }");
  BorderedFrame w;
  w.show();

  return app.exec();
}

#include "main.moc"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM