多个QtWidget层叠显示,置顶,置底,Lbel显示图片

多个QtWidget层叠显示,置顶,置底,Lbel显示图片

把多个widget放入QStackedLayout
QStackedLayout设置成setStackingMode(QStackedLayout::StackAll);

解释:
enum QStackedLayout::StackingMode
This enum specifies how the layout handles its child widgets regarding their visibility. Constant Value Description
QStackedLayout::StackOne 0 Only the current widget is visible. This is the default.
QStackedLayout::StackAll 1 All widgets are visible. The current widget is merely raised.

/////.h

#ifndef MAINWINDOW_H

#define MAINWINDOW_H
 
  
#include 
#include "MCanvas.h"
 
  
class MainWindow : public QMainWindow
{
    Q_OBJECT
 
  
public:
    MainWindow(QWidget *parent = 0);
    ~MainWindow();
 
  
    
    MCanvas * canvs;
};
 
  
#endif // MAINWINDOW_H
 
  

////////.cpp

#include "MainWindow.h"

#include
#include 
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{

setWindowTitle("canvastest");

    setWindowIcon(QIcon("F:\\media\Image\\3.jpg"));
 
  
    QWidget *CentralWidget = new QWidget(this);
 
  
    QString imagepath = "F:\\media\\Image\\3.jpg";
    QPixmap pixmap(imagepath);
    QPixmap scaledPixmap = pixmap.scaled(QSize(500,500),Qt::KeepAspectRatio);
 
  
    QLabel *lblResultImage = new QLabel(CentralWidget);
    lblResultImage->setPixmap(scaledPixmap);
 
  
 
  
    canvs = new MCanvas(CentralWidget);
    canvs->setPenColor(0,0,0,255);
 
  
    canvs->raise();
    canvs->setWindowFlags(Qt::WindowStaysOnTopHint);
    lblResultImage->setWindowFlags(Qt::WindowStaysOnBottomHint);
 
  
 
  
    QStackedLayout * sBoxLayout = new QStackedLayout();
    sBoxLayout->setStackingMode(QStackedLayout::StackAll);
    sBoxLayout->addWidget(lblResultImage);
    sBoxLayout->addWidget(canvs);
    CentralWidget->setLayout(sBoxLayout);
 
  
    this->setCentralWidget(CentralWidget);
    canvs->raise();
    canvs->setWindowFlags(Qt::WindowStaysOnTopHint);//置顶
    lblResultImage->setWindowFlags(Qt::WindowStaysOnBottomHint);//置底
 
  
//    lblResultImage->raise();
//    lblResultImage->setWindowFlags(Qt::WindowStaysOnTopHint);
//    canvs->setWindowFlags(Qt::WindowStaysOnBottomHint);

}
 
  
MainWindow::~MainWindow()
{
 
  
}


你可能感兴趣的:(Qt)