2014-12-02 90 views
0

我坚持一个简单的Qt函数,不适用于我。我做了一个继承自QMainWindow的 的类和另一个从QWidget继承的类。然后我从第二个成员对象(一个指向)的第一个元素,并在构建窗口的过程中将其指定为其中央控件。Qt QWidget :: setGeomerty

当谈到调整自己与功能QWidget的:: setGeomerty(在窗口内centraWidget)它根本就没有work.here是我的代码:

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(); 

} 

我知道这可能是愚蠢的,但我只是无法想象它。帮助我。

+0

通常你会打电话来的''MainWindow'我setGeometry'认为。 – sje397 2014-12-02 04:16:14

+0

你知道如何调整主窗口内的cantralwidget吗?我只是尝试了Move()和resize()而不是setGeometry(),它不起作用。 – coucou8949 2014-12-02 04:27:17

+0

添加布局? http://qt-project.org/doc/qt-4.8/layout.html – sje397 2014-12-02 04:28:06

回答

0

QMainWindow有其自己的布局,其中央区域被中央小部件占据。因此,打破布局并随意修改中央小部件的大小/位置并不是非常简单。 我推荐的是使用一个占位符中心小部件,并将您的小部件添加为小孩。 我很肯定你可以通过在“占位符”中央小部件中设置一个合适的Qt内置布局,然后将小部件添加到布局来达到你想要的效果。

+0

我想我只是在做你刚刚说的话。谢谢。吼叫是我的新的和最终的代码: – coucou8949 2014-12-02 12:13:42

0

布局是管理我想要的;我认为这是不可能的,直接在中央物件交出,并尝试移动/调整它;但通过添加布局和子部件,我们可以处理them.here的是我的代码:

mainwindow.cpp

//我专注于我的窗口构造

的MainWindow :: MainWindow的(QWidget的*父):
的QMainWindow(父),
m_MainView(新的MainView(本)),
m_WindowLayout(新MainWindLayout(NULL ))/ /我的自定义布局女巫只是从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. 

}

谢谢大家