gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:59:58 25 4
gpt4 key购买 nike

 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

最佳答案

我怀疑问题出在顶层布局,也就是winLayout。将 QMainWindow 的中心部件设置为 winLayout 的父部件:

winLayout = new QVBoxLayout(ui->centralWidget);

我推荐使用 Qt Creator 或 Qt Designer 来设计用户界面。 Qt Creator 为布局和其他无趣的东西创建必要的代码。即使您决定通过编写自己的代码来创建用户界面,您也可以使用 Qt Creator 创建原型(prototype)并查看它创建的代码类型。

关于c++ - 为什么 QT 中的布局管理器不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3798184/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com