views:

75

answers:

1
 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

+1  A: 

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.

Roku
Thanks for your answering! When I using the Qt Creator, the loyouts on the design surface are really hardly too deal with. It really makes me annoyed!
Liu