如何用QT绘制平铺图像

Max*_*rai 3 c++ qt image

我正在QtCreator的设计师中编写C++/Qt界面.用一些背景图像选择制作哪个元素作为矩形?

第二个问题:如何绘制平铺图像?我有和大小(1×50)的图像,我想渲染它的父宽度.有任何想法吗?


mTopMenuBg = QPixmap("images/top_menu_bg.png");
mTopMenuBrush = QBrush(mTopMenuBg);
mTopMenuBrush.setStyle(Qt::TexturePattern);
mTopMenuBrush.setTexture(mTopMenuBg);

ui->graphicsView->setBackgroundBrush(mTopMenuBrush);
Run Code Online (Sandbox Code Playgroud)

QBrush:TexturePattern的使用不正确

Dus*_*ell 7

如果您只想显示图像,可以使用QImage.要使图像平铺为背景,使用QImage 构建QBrush.然后,如果您使用的是QGraphicsScene,则可以将bursh设置为背景画笔.

这是一个用平铺图像"document.png"填充整个主窗口的示例:

int main(int argc, char *argv[]) {
    QApplication app(argc, argv);
    QMainWindow *mainWindow = new QMainWindow();

    QGraphicsScene *scene = new QGraphicsScene(100, 100, 100, 100);
    QGraphicsView *view = new QGraphicsView(scene);
    mainWindow->setCentralWidget(view);

    QImage *image = new QImage("document.png");
    if(image->isNull()) {
        std::cout << "Failed to load the image." <<std::endl;
    } else {
        QBrush *brush = new QBrush(*image);
        view->setBackgroundBrush(*brush);
    }

    mainWindow->show();
    return app.exec();
}
Run Code Online (Sandbox Code Playgroud)

结果应用:
屏幕截图

或者,您似乎可以将样式表与任何窗口小部件一起使用,并更改窗口小部件上的background-image属性.这与QtDesigner有更多的集成,因为您可以在QtDesigner中设置样式表和图像.