我被一个不适用于我的Qt的简单函数困住了,我创建了一个继承自QMainWindow的类和另一个从QWidget.then继承的类,我从第二个创建了一个成员对象(指向)第一个成员对象(一个指向),并在构造窗口期间将它指定为它的centralWidget。
当使用函数QWidget::setGeomerty()调整窗口内的centraWidget时,它根本不工作。下面是我的代码:
void MainWindow::show()
{
//some code that centers my window on the screen
int margin=this->width()/7;
centralWidget()->setGeometry(margin,centralWidget()->geometry().top(),this->width()-margin,centralWidget()->geometry().bottom());
QMainWindow::show();
}我知道这可能很愚蠢,但我只是不明白out.help我自己。
发布于 2014-12-02 05:22:56
QMainWindow有自己的布局,中心区域被中央小部件占用。因此,打破布局并任意修改中央小部件大小/位置并不是很简单。我建议的是使用占位符中央小部件并将您的小部件作为子部件添加。我非常肯定,您可以通过将适当的内置布局Qt设置到“占位符”中心小部件,然后将您的小部件添加到布局中,从而实现您想要的结果。
发布于 2014-12-02 12:25:35
布局是管理我想要的东西所必需的;我认为不可能直接交出中心小部件并试图移动/调整它的大小;但是通过添加布局和子小部件,我们可以释放them.here是我的代码:
mainwindow.cpp
//我专注于我的窗口构造函数
主窗口::主窗口(QWidget*父):
QMainWindow(parent), m\_MainView(new MainView(this)), m\_WindowLayout(new MainWindLayout(NULL))//my custom layout witch just inherits from QGridLayout{
//.............
setCentralWidget(m_MainView);
m_WindowLayout=new MainWindLayout(m_MainView);//my layout set into my central Widget"m_MainView".
QWidget *temp(dynamic_cast<QWidget *>(m_MainView->centralView()));//centralView() returns a child QWidget of my central Widget
QMargins margins(this->width()/5,0,this->width()/5,0);//setting up layout margins :left,top,right,bottom;exactly what i need
m_WindowLayout->setContentsMargins( margins);
m_WindowLayout->addWidget(temp,0,0,-1,-1);//adding my child widget to the layout filling all cells of the gridlayout.}
谢谢大家
https://stackoverflow.com/questions/27241763
复制相似问题