所以你不能再给mainwindow添加QVBoxLayout了,你只能给centralWidget添加layout。
只要将setLayout(mainLayout)
这一句改成:
this->centralWidget()->setLayout(mainLayout)就行了。
改过之后:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this)
QVBoxLayout *mainLayout = new QVBoxLayout
Title = new QLabel(tr("校园导航系统"),this)
Title->resize(550,100)
Title->setAlignment(Qt::AlignCenter)
Title->setStyleSheet("background-color:redfont-size:40pxcolor:blue")
Greeting = new QLabel(tr("Welcome"),this)
Greeting->resize(550,100)
Greeting->setStyleSheet("background-color:yellowfont-size:20pxcolor:blue")
mainLayout->addWidget(Title)
mainLayout->addWidget(Greeting)
this->centralWidget()->setLayout(mainLayout)
//++++++++
setWindowTitle(tr("校园导航系统"))
resize(550,600)
}
VB2011将VB6的通用对话框控件分解成为OpenFileDialog、SaveFileDialog、ColorDialog 和 FontDialog 组件处理文件、颜色和字体,直接在工具箱面板上找到对应组件,双击加入窗体,即可使用。
实现步骤:1、设置setStyleSheet的样式
Btn->setStyleSheet("QToolButton{border:Opx}")
2、设置按钮的尺寸自适应图片大小
Btn->setIconSize(QSize(Btn->width(),Btn->height()))
3、新建QPixmap类,并加载按钮的图片背景图片
QPixmap pixmap(path_image)
Btn->setIcon(QIcon(pixmap))
程序:
widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include "QImage"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this)
QImage image
image.load(":/image/6631506470840007614.jpg")
QPalette palette
palette.setBrush(this->backgroundRole(),QBrush(image))
this->setPalette(palette)
setBtnIcon(ui->open, ":/image/open.png")
setBtnIcon(ui->large, ":/image/big.png")
setBtnIcon(ui->litte, ":/image/litte.png")
setBtnIcon(ui->next, ":/image/next.png")
setBtnIcon(ui->previous, ":/image/previous.png")
setBtnIcon(ui->normal, ":/image/normal.png")
setBtnIcon(ui->recovery, ":/image/recover.png")
setBtnIcon(ui->start, ":/image/start.png")
setBtnIcon(ui->stop, ":/image/stop.png")
setBtnIcon(ui->rotate, ":/image/zhuan.png")
setBtnIcon(ui->open, ":/image/open.png")
}
Widget::~Widget()
{
delete ui
}
void Widget::setBtnIcon(QToolButton *Btn, const QString &path_image)
{
Btn->setStyleSheet("QToolButton{border:Opx}")
Btn->setIconSize(QSize(Btn->width(),Btn->height()))
QPixmap pixmap(path_image)
Btn->setIcon(QIcon(pixmap))
}
2、widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include "QToolButton"
#include "QLabel"
#include "QTimer"
#include "QString"
#include "QPixmap"
#include "QPalette"
#include "QMatrix"
namespace Ui {
class Widget
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0)
~Widget()
private:
void setBtnIcon(QToolButton *Btn, const QString &path_image)
private:
Ui::Widget *ui
}
#endif // WIDGET_H
QLabel的确是可以和QWidget一样做父控件。QLabel w
QPushButton button(&w)
QLabel w
QWidget widget
widget.setParent(&w)
QLabel label(&W)
w.show()
都试过了,是可以正常显示的,和QWidget作父控件是一样的效果。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)