现在的位置: 首页 > 综合 > 正文

Qt学习笔记3

2013年06月04日 ⁄ 综合 ⁄ 共 2439字 ⁄ 字号 评论关闭

滴3章:创建主窗口

1.当槽作为一个信号的响应函数而被执行时,就会忽略这个返回值;但是当把槽作为函数来调用时,其返回值对我们的作用就和调用任何一个普通的c++函数时的作用相同。

2.资源机制:

  创建一个资源文件:xxx.qrc , 在.pro下添加一行:RESOURCES = spreadsheet.qrc ,然后资源的配置都可以在xxx.qrc中配了,如下:

<RCC>

<qresource>

 <file>images/icon.png</file>

</qresource>

</RCC> 所有资源文件都会被编译到应用程序可执行文件中,因此并不会弄丢他们。当引用这些资源时,需要使用带路径前缀 :/ 的形式 =》:/images/icon.png的形式。

Spreadsheet *spareadsheet;

FindDialog *findDialog;

QLabel *locationLabel;

QStringList recentFiles;

QString curFile;

QAction xxx;

QMenu *fileMenu;

QToolBar *fileToolBar;

3.创建菜单和工具拦:

 qt通过“动作“的概念简化了有关菜单和工具栏的编程。一个动作就是一个可以添加到任意数量的菜单和工具栏的项。

****动作如下创建;(菜单项目的子项、上下文右键菜单的项、工具条等,其子项都是一个Action.通用的。)

newAction = new QAction( tr("&New"), this );  加速键&N, 父对象:主窗口,

newAction->setIcon( QIcon(":/images/new.png") ); 图标

newAction->setShortcut(QKeySequence::New); 快捷键 or tr("Ctrl+Q")这样写也是可以的。

newAction ->setStatusTip( tr("create a new spreadsheet file")); 状态提示

connect( newAction, SIGNAL( triggered() ), this, SLOT(newFile()) ); triggered()信号 绑定 this的newFile()槽。

4.spreadsheet控件可以显示网格,设置方法为:setShowGrid(bool).                         aboutQt();

****创建菜单如下:

fileMenu = menuBar() -> addMenu( tr("&File"); // 添加菜单项,menuBar()在第一次调用的时候就会创建出菜单来,并且返回一个菜单栏指针。

fileMenu = addAction(newAction); //  添加动作到该项下

separatorAction = fileMenu -> addSeparator(); // 添加一个间隔条,并返回一个间隔条指针

---------

menuBar()返回一个菜单栏指针,

addMenu()返回一个菜单栏项指针,

addSeparator()返回一个间隔器指针,

------------------------------

上下文菜单创建:

spreadSheet = new Spreadsheet; // 电子表格类的控件。table类

setCentralWidget(spreadsheet);设置为中央部件

spreadSheet->addAction(cutAction); 添加动作

spreadsheet->setContextMenuPolicy(Qt::ActionsContextMenu);设置为上下文

一种更高级的上下文菜单方法是重新实现QWidget::contextMenuEvent()函数,创建一个QMenu窗口部件,在其中添加所期望的那些动作,并且再对该窗口不见调用exec()函数。

-----------------------

工具栏的创建:

fileToolBar = addToolBar(tr("&File"));

fileToolBar->addAction(newAction); xx->addSeparator();与菜单兰类似

设置状态烂:

statusBar()->addWidget(locationLabel1); 该label自动更换父对象为statusBar.

locationLabel->setAlignment(Qt::AlignHCenter); // 水平方向向上居中

locationLabel->setMinimumSize(locationLabel->sizeHint() ) ; //在状态栏的子项,最好先用sizeHint()理想大小。page44

statusBar()->addWidget(formulaLabel, 1 );不让它被延森,指定一个伸展因子1而实现的。

connect(spreadsheet, SIGNAL(currentCellChanged(int,int,int,int)), this, SLOT(updateStatusBar()) ); 

connect(spreadsheet, SIGNAL(modified() ), this, SLOT(spreadsheetModified() ) );

----------

void MainWindow::updateStatusBar()

{

  locationLabel->setText(spreadsheet->currentLocation());  spareadsheet->currentFormula();

}

spreadsheet()不会一开始的时候就发射currentCellChanged()消息,我们必须还要以下做:吧windowModified属性设置为true用以更新标题栏。

void MainWindow::spreadsheetModified()

{

  setWindowModified(true);  updateStatusBar;

}

抱歉!评论已关闭.