为什么 QT 中的布局管理器不起作用?
this->setWindowTitle(tr("数据转移程序"));
edt_ftp_server = new QLineEdit;
edt_ftp_port = new QLineEdit;
edt_ftp_account = new QLineEdit;
edt_ftp_pwd = new QLineEdit;
edt_ftp_pwd->setEchoMode( QLineEdit::Password );
lbl_ftp_server = new QLabel;
lbl_ftp_server->setText(tr("FTP服务器地址:"));
lbl_ftp_server->setBuddy( edt_ftp_server );
lbl_ftp_port = new QLabel;
lbl_ftp_port->setText(tr("FTP服务器端口:"));
lbl_ftp_port->setBuddy( edt_ftp_port );
lbl_ftp_account = new QLabel;
lbl_ftp_account->setText(tr("FTP登录帐号:"));
lbl_ftp_account->setBuddy( edt_ftp_account );
lbl_ftp_pwd = new QLabel;
lbl_ftp_pwd->setText(tr("FTP登录密码:"));
lbl_ftp_pwd->setBuddy( edt_ftp_pwd );
ftp_settings = new QGroupBox(this);
ftp_settings->setTitle(tr("FTP服务器设置"));
ftp_settingsLayout = new QGridLayout;
ftp_settingsLayout->addWidget( lbl_ftp_server, 0, 0);
ftp_settingsLayout->addWidget( edt_ftp_server, 0, 1);
ftp_settingsLayout->addWidget( lbl_ftp_port, 1, 0);
ftp_settingsLayout->addWidget( edt_ftp_port, 1, 1);
ftp_settingsLayout->addWidget( lbl_ftp_account, 2, 0);
ftp_settingsLayout->addWidget( edt_ftp_account, 2, 1);
ftp_settingsLayout->addWidget( lbl_ftp_pwd, 3, 0);
ftp_settingsLayout->addWidget( edt_ftp_pwd, 3, 1);
ftp_settings->setLayout( ftp_settingsLayout );
edt_db_server = new QLineEdit( this );
edt_db_port = new QLineEdit( this );
edt_db_account = new QLineEdit( this );
edt_db_pwd = new QLineEdit( this );
edt_db_pwd->setEchoMode( QLineEdit::Password );
lbl_db_server = new QLabel( this );
lbl_db_server->setText(tr("FTP服务器地址:"));
lbl_db_server->setBuddy( edt_ftp_server );
lbl_db_port = new QLabel( this );
lbl_db_port->setText(tr("FTP服务器端口:"));
lbl_db_port->setBuddy( edt_ftp_port );
lbl_db_account = new QLabel( this );
lbl_db_account->setText(tr("FTP登录帐号:"));
lbl_db_account->setBuddy( edt_ftp_account );
lbl_db_pwd = new QLabel( this );
lbl_db_pwd->setText(tr("FTP登录密码"));
lbl_db_pwd->setBuddy( edt_ftp_pwd );
db_settings = new QGroupBox(this);
db_settings->setTitle(tr("数据库服务器设置"));
db_settingsLayout = new QGridLayout;
db_settingsLayout->addWidget( lbl_ftp_server, 0, 0);
db_settingsLayout->addWidget( edt_ftp_server, 0, 1);
db_settingsLayout->addWidget( lbl_ftp_port,1, 0);
db_settingsLayout->addWidget( edt_ftp_port,1, 1);
db_settingsLayout->addWidget( lbl_ftp_account, 2, 0);
db_settingsLayout->addWidget( edt_ftp_account, 2, 1);
db_settingsLayout->addWidget( lbl_ftp_pwd,3, 0);
db_settingsLayout->addWidget( edt_ftp_pwd, 3, 1);
db_settings->setLayout( db_settingsLayout );
buttonsLayout = new QHBoxLayout;
buttonsLayout->addStretch();
btn_start = new QPushButton;
btn_start->setText(tr("开始"));
buttonsLayout->addWidget(btn_start);
btn_stop = new QPushButton;
btn_stop->setText(tr("停止"));
buttonsLayout->addWidget( btn_stop );
btn_exit = new QPushButton;
btn_exit->setText(tr("退出"));
buttonsLayout->addWidget(btn_exit);
settingLayout = new QVBoxLayout;
settingLayout->addWidget( db_settings );
settingLayout->addStretch();
settingLayout->addWidget( ftp_settings );
centralLayout = new QHBoxLayout;
centralLayout->addLayout( settingLayout );
lst_log = new QListWidget;
centralLayout->addWidget(lst_log);
winLayout = new QVBoxLayout;
winLayout->addLayout( centralLayout );
winLayout->addLayout( buttonsLayout );
setLayout( winLayout );
我正在开发一个小型的 qt 程序,并在 QMainWindow 子类构造函数中编写了上面的代码。 但显示的小部件很混乱。感谢所有建议!
以下是结果截图:
this->setWindowTitle(tr("数据转移程序"));
edt_ftp_server = new QLineEdit;
edt_ftp_port = new QLineEdit;
edt_ftp_account = new QLineEdit;
edt_ftp_pwd = new QLineEdit;
edt_ftp_pwd->setEchoMode( QLineEdit::Password );
lbl_ftp_server = new QLabel;
lbl_ftp_server->setText(tr("FTP服务器地址:"));
lbl_ftp_server->setBuddy( edt_ftp_server );
lbl_ftp_port = new QLabel;
lbl_ftp_port->setText(tr("FTP服务器端口:"));
lbl_ftp_port->setBuddy( edt_ftp_port );
lbl_ftp_account = new QLabel;
lbl_ftp_account->setText(tr("FTP登录帐号:"));
lbl_ftp_account->setBuddy( edt_ftp_account );
lbl_ftp_pwd = new QLabel;
lbl_ftp_pwd->setText(tr("FTP登录密码:"));
lbl_ftp_pwd->setBuddy( edt_ftp_pwd );
ftp_settings = new QGroupBox(this);
ftp_settings->setTitle(tr("FTP服务器设置"));
ftp_settingsLayout = new QGridLayout;
ftp_settingsLayout->addWidget( lbl_ftp_server, 0, 0);
ftp_settingsLayout->addWidget( edt_ftp_server, 0, 1);
ftp_settingsLayout->addWidget( lbl_ftp_port, 1, 0);
ftp_settingsLayout->addWidget( edt_ftp_port, 1, 1);
ftp_settingsLayout->addWidget( lbl_ftp_account, 2, 0);
ftp_settingsLayout->addWidget( edt_ftp_account, 2, 1);
ftp_settingsLayout->addWidget( lbl_ftp_pwd, 3, 0);
ftp_settingsLayout->addWidget( edt_ftp_pwd, 3, 1);
ftp_settings->setLayout( ftp_settingsLayout );
edt_db_server = new QLineEdit( this );
edt_db_port = new QLineEdit( this );
edt_db_account = new QLineEdit( this );
edt_db_pwd = new QLineEdit( this );
edt_db_pwd->setEchoMode( QLineEdit::Password );
lbl_db_server = new QLabel( this );
lbl_db_server->setText(tr("FTP服务器地址:"));
lbl_db_server->setBuddy( edt_ftp_server );
lbl_db_port = new QLabel( this );
lbl_db_port->setText(tr("FTP服务器端口:"));
lbl_db_port->setBuddy( edt_ftp_port );
lbl_db_account = new QLabel( this );
lbl_db_account->setText(tr("FTP登录帐号:"));
lbl_db_account->setBuddy( edt_ftp_account );
lbl_db_pwd = new QLabel( this );
lbl_db_pwd->setText(tr("FTP登录密码"));
lbl_db_pwd->setBuddy( edt_ftp_pwd );
db_settings = new QGroupBox(this);
db_settings->setTitle(tr("数据库服务器设置"));
db_settingsLayout = new QGridLayout;
db_settingsLayout->addWidget( lbl_ftp_server, 0, 0);
db_settingsLayout->addWidget( edt_ftp_server, 0, 1);
db_settingsLayout->addWidget( lbl_ftp_port,1, 0);
db_settingsLayout->addWidget( edt_ftp_port,1, 1);
db_settingsLayout->addWidget( lbl_ftp_account, 2, 0);
db_settingsLayout->addWidget( edt_ftp_account, 2, 1);
db_settingsLayout->addWidget( lbl_ftp_pwd,3, 0);
db_settingsLayout->addWidget( edt_ftp_pwd, 3, 1);
db_settings->setLayout( db_settingsLayout );
buttonsLayout = new QHBoxLayout;
buttonsLayout->addStretch();
btn_start = new QPushButton;
btn_start->setText(tr("开始"));
buttonsLayout->addWidget(btn_start);
btn_stop = new QPushButton;
btn_stop->setText(tr("停止"));
buttonsLayout->addWidget( btn_stop );
btn_exit = new QPushButton;
btn_exit->setText(tr("退出"));
buttonsLayout->addWidget(btn_exit);
settingLayout = new QVBoxLayout;
settingLayout->addWidget( db_settings );
settingLayout->addStretch();
settingLayout->addWidget( ftp_settings );
centralLayout = new QHBoxLayout;
centralLayout->addLayout( settingLayout );
lst_log = new QListWidget;
centralLayout->addWidget(lst_log);
winLayout = new QVBoxLayout;
winLayout->addLayout( centralLayout );
winLayout->addLayout( buttonsLayout );
setLayout( winLayout );
I am developing a tiny qt program, and have writen above code in a QMainWindow subclass Constructor.
But the widgets displayed are messed up.All advices are appreciated!
The following is the result screenshot:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我怀疑问题出在顶层布局,即winLayout。将 QMainWindow 的中央小部件设置为 winLayout 的父级:
我建议使用 Qt Creator 或 Qt Designer 来设计用户界面。 Qt Creator 为布局和其他无趣的事情创建必要的代码。即使您决定通过编写自己的代码来创建用户界面,您也可以使用 Qt Creator 创建原型并查看它创建的代码类型。
I suspect that the problem lies in the top level layout, which is winLayout. Set QMainWindow's central widget to winLayout's parent:
I recommend using Qt Creator or Qt Designer for designing the user interfaces. Qt Creator creates the necessary code for layouts and other uninteresting things. And even if you decide to create user interfaces by writing your own code, you can create a prototype with Qt Creator and look what kind of code it creates.