0:QMdiArea中添加子窗口后,想固定or调整窗口的大小
QImage colorImage(filename)
QLabel * imagelabel =new QLabel
imagelabel->setPixmap(QPixmap::fromImage(colorImage))
imagelabel->setAttribute(Qt::WA_DeleteOnClose)
imagelabel->setWindowTitle(title)
imagelabel->setFixedSize(colorImage.size())
imagelabel->setMaximumSize(colorImage.size())
imagelabel->setMinimumSize(colorImage.size())
ui->mdiArea->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding)
QMdiSubWindow *sw =ui->mdiArea->addSubWindow(imagelabel)
sw->setFixedSize(colorImage.size().width()+10,colorImage.size().height()+30)
imagelabel->show()
1:重写QPressEvent and QMoveEvent实现在无框架窗口中拖动widget时,主窗口位置移动
void Form::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton) {
dragPosition = event->globalPos() - frameGeometry().topLeft();
event->accept();
}
}
void Form::mouseMoveEvent(QMouseEvent *event)
{
if (event->buttons() & Qt::LeftButton)
{
move(event->globalPos() - dragPosition);
event->accept();
}
}
2:在主窗口中重写 bool eventFilter(QObject target, QEvent e)
实现方法也非常容易:
##1:在构造函数中注册相关子widget,这样主窗口就可以事先捕获到本来应该传递给子widget的事件
ui->pushButton->installEventFilter(this);
##2:在eventFilter(QObject *target, QEvent *e)函数中,实现对子窗口事件的处理
如下为一个例子:
bool eventFilter(QObject *target, QEvent *e)
{
if(target == ui->pushButton)
{
QMouseEvent *temp=(QMouseEvent *)e;
if(e->type() == QEvent::MouseButtonPress)
{
qcout<<"mouse pressed"<<endl;
pos = temp->globalPos() - frameGeometry().topLeft();
temp->accept();
}
if(e->type()== QEvent::MouseMove)
{
qcout<<"mosue moved"<<endl;
move(temp->globalPos() - pos);
temp->accept();
}
}
return false;
}
##关于QEvent的type(),我们可以看下面的网址(当然在Qt官网查也是可以的):
http:
4:实现主窗口透明,子widet不透明的方法
##1:构造函数中添加如下函数
setWindowFlags(Qt::FramelessWindowHint);
setAttribute(Qt::WA_TranslucentBackground, true);
##2:void paintEvent()函数重写
protected:
void paintEvent(QPaintEvent *event)
{
QPainter painter(this);
painter.fillRect(this->rect(), QColor(0, 0, 255, 80));
}
3:QPushButton在按下后不显示边框
ui->pushButton->setStyleSheet("border-style:hidden;");
沒有留言:
張貼留言