为什么 QT 中的布局管理器不起作用?

发布于 2024-09-24 23:23:12 字数 3672 浏览 3 评论 0原文

 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 子类构造函数中编写了上面的代码。 但显示的小部件很混乱。感谢所有建议!

以下是结果截图:

alt text

 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:

alt text

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

硪扪都還晓 2024-10-01 23:23:12

我怀疑问题出在顶层布局,即winLayout。将 QMainWindow 的中央小部件设置为 winLayout 的父级:

winLayout = new QVBoxLayout(ui->centralWidget);

我建议使用 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:

winLayout = new QVBoxLayout(ui->centralWidget);

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文