heghost |
2013-08-24 22:52 |
将书中源码spreadsheet修改为多文档界面时出现令人费解的问题
书中给出了实现spreadsheet的源码,但书中对spreadsheet实现多文档界面的描述似乎过于简单,在csdn看到对spreadsheet的功能进行拓展的代码(实现了多文档界面,加入了redo和undo操作)。在spreadsheet源码的spreadsheet的构造函数中,使用了这样一种方式: Spreadsheet::Spreadsheet(QWidget *parent): QTableWidget(parent) { //代码省略,通过调用 setRowCount(RowCount)和 setColumnCount(ColumnCount)设置行数和列数, //其中RowCount和ColumnCount都是枚举常量 } 为了能够指定表的行数和列数,我将spreadsheet的构造函数改成这样: Spreadsheet::Spreadsheet(int row, int col, QWidget *parent) : QTableWidget(row, col, parent) { } 也就是在创建表的时候指定行数和列数。 这样修改后运行程序,发现工具栏中剪切、复制、删除都处于非激活状态,如附件图片所示。最小化程序,然后再恢复窗口后就处于激活状态了。这种问题就非常诡异了,一直没想通............. 单步跟踪发现: 单击最小化和然后再恢复窗口时,通过connect(mdiArea, SIGNAL(subWindowActivated(QMdiSubWindow*)), this, SLOT(updateActions(QMdiSubWindow*))); 进入updateActions函数 void MainWindow::updateActions(QMdiSubWindow *subWindow){ Spreadsheet *sp = activeSpreadsheet(); bool hasSpreadsheet = sp; bool hasSelection = hasSpreadsheet && !sp->selectedRanges().isEmpty(); bool hasTextOnClipboard = !(QApplication::clipboard()->text().isEmpty()); saveAction->setEnabled(hasSpreadsheet); saveAsAction->setEnabled(hasSpreadsheet); cutAction->setEnabled(hasSelection); //部分代码省略 } sp指空,导致工具栏不激活。这种问题在Spreadsheet的构造函数使用前面叙述的第一种方法时就不会出现。 补充:activeSpreadsheet()函数定义如下: Spreadsheet* MainWindow::activeSpreadsheet(){ QMdiSubWindow *subWindow = mdiArea->activeSubWindow(); if (subWindow) return qobject_cast<Spreadsheet *>(subWindow->widget()); return 0;}
|
|