qt 编程技巧(2)

4.源代码中引用方式:[b]setWindowIcon/b;
或者 [b]setWindowIcon/b;

创建菜单栏、工具栏、状态栏
void MainWin::createActions()
{
newAct = QAction(tr(“&New”), this); // 加速键N
newAct->setIcon(QIcon(“images/new.png”)); // 图标
newAct->setShortcut(QKeySequence::New); // 快捷键 Ctrl+N
// newAct->setShortcut(“Ctrl+N”); // 快捷键 Ctrl+N
newAct->setStatusTip(tr(“Create a new file”)); // 状态提示
connect(newAct, SIGNAL(triggered()), this, newFile());

showGridAct->setCheckable(true); // 带复选框的菜单
showGridAct->setChecked(true); // 选中

}
// 菜单栏
void MainWin::createMenus()
{
fileMenu = menuBar()->addMenu(tr(“&File”)); // file菜单
fileMenu->addAction(newAct); // 添加到菜单中

//QAction *separatorAct;
fileMenu->addSeparator(); // 添加间隔器

editMenu = menuBar()->addMenu(tr(“&Edit”)); // edit菜单
QMenu *subMenu = editMenu->addMenu(tr(“&Select”)); // 添加子菜单
subMenu->addAction(…);

}
// 工具栏
void MainWin::createToolBars()
{
fileToolBar = addToolBar(tr(“&File”));
fileToolBar->addAction(newAct);

editToolBar = addToolBar(tr(“&Edit”));
editToolBar = addAction(…);
editToolBar = addSeparator();

}
// 状态栏
void MainWin::createStatusBar()
{
locationLabel = new QLabel(tr(" W999 "));
locationLabel->setAlignment(Qt::AlignHCenter);
locationLabel->setMinimumSize(locationLabel->sizeHint());

otherLabel = new QLabel;
otherLabel->setIndent(3); // 缩进3个字符

statusBar()->addWidget(locationLabel);
statusBar()->addWidget(otherLabel, 1); // 窗口改变时,伸展它
}

模态对话框与非模态对话框
模态对话框典型例子:打开文件对话框,警告对话框
非模态对话框典型例子:查找对话框
模态对话框一般在堆中创建,非模态对话框一般在栈中创建
模态对话框使用exec()显示,非模态对话框使用show()显示
void MainWin::find()
{
if (!findDlg) // 不存在,创建它
{
findDlg = new FindDlg(this);
}
findDlg->show(); // 显示,并且是非模态的
findDlg->raise(); // 位于最上方
findDlg->activateWindow(); // 激活
}
void MainWin::goTo()
{
GoToDlg dlg(this);
if (dlg.exec()) // 模态的
{
// 对话框返回true(QDialog::Accepted)

}
// 函数结束时,自动销毁对话框
}

创建一个启动画面
int main(…)
{
QApplication app(…);
QSplashScreen *splash = new QSplashScreen;
splash->setPixmap(QPixmap(“:/images/splash.png”));
splash->show();
app.processEvents(); // 处理点击启动画面的事件
splash->showMessage(QObject::tr(“XXXX YYYYY …”),
Qt::AlignRight|Qt::AlignTop, Qt::white);
MainWin win;
splash->showMessage(…);
initNetwork(…);

win.show();
splash->finish(&win);
delete splash;

return app.exec();
}

技术帖子顶起来嘿嘿,好东西多来!!!:4_102: