2011-04-25 144 views
3

我在QtCreator的设计器中用C++/Qt编写接口。选择什么元素作为背景图像的矩形?如何用QT绘制平铺图像

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


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

ui->graphicsView->setBackgroundBrush(mTopMenuBrush); 

QBrush:不正确地使用 TexturePattern的

回答

7

如果你只是想显示的图像可以使用QImage。用图像平铺构建一个带有QImage的QBrush的背景。然后,例如,如果您使用的是QGraphicsScene,则可以将背景刷设置为背景刷。

这里是填补与平铺图像“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(); 
} 

生成的应用程序:
screen shot

另外,似乎你可以使用style sheets与任何小部件并更改小部件上的background-image属性。这与QtDesigner更加集成,因为您可以在QtDesigner中设置样式表和图像。

+0

对不起,无法理解如何使用该笔刷填充窗口中的确切矩形。并在哪里画?在'paintEvent'中? – Ockonal 2011-04-25 16:32:01

+0

你只是想用平铺图像填满一个窗口吗?你想要把图像放在什么样的小部件上? – 2011-04-25 17:01:53

+0

@ dusty-campbell是的,我只需要在平铺纹理的主窗口填充一些矩形。我不在乎使用什么帮助小工具。 – Ockonal 2011-04-25 17:04:48