C++ 选项卡化所有dock小部件
我正在尝试创建一个菜单操作,它将在我的qMain窗口中对所有停靠点进行选项卡化,以减少混乱。在尝试使用迭代器进行一些算术运算之后,oops!,我意识到我需要使用类似于。问题是,它不会在我的应用程序中编译,因为dockPair没有初始化。我真的不太了解相邻迭代器的实现或限制,也不确定我是否做错了什么,或者它是否真的不适合我的应用程序 此外,如果有一种比我的方法更容易/更好的方法来对所有码头进行选项卡化,我可以重新实现该功能 tabify码头:C++ 选项卡化所有dock小部件,c++,qt,c++11,iterator,C++,Qt,C++11,Iterator,我正在尝试创建一个菜单操作,它将在我的qMain窗口中对所有停靠点进行选项卡化,以减少混乱。在尝试使用迭代器进行一些算术运算之后,oops!,我意识到我需要使用类似于。问题是,它不会在我的应用程序中编译,因为dockPair没有初始化。我真的不太了解相邻迭代器的实现或限制,也不确定我是否做错了什么,或者它是否真的不适合我的应用程序 此外,如果有一种比我的方法更容易/更好的方法来对所有码头进行选项卡化,我可以重新实现该功能 tabify码头: 问题是,我正试图用MSVC2010编译这段代码,它是
问题是,我正试图用MSVC2010编译这段代码,它是 使用normal for循环的工作代码如下:
void mainWindow::tabifyDocks()
{
// get a list of all the docks
QList<QDockWidget*> docks = findChildren<QDockWidget*>();
// first, un-float all the tabs
std::for_each(docks.begin(), docks.end(), std::bind(&QDockWidget::setFloating, std::placeholders::_1 /* the dock widget*/, false));
// sort them into dockWidget areas
QVector<QDockWidget*> topArea, leftArea, rightArea, bottomArea;
QVector<QVector<QDockWidget*>*> dockAreas;
dockAreas.push_back(&topArea);
dockAreas.push_back(&leftArea);
dockAreas.push_back(&rightArea);
dockAreas.push_back(&bottomArea);
std::for_each(docks.begin(), docks.end(), [&] (QDockWidget* dock)
{
if (dockWidgetArea(dock) == Qt::TopDockWidgetArea ) {topArea. push_back(dock); this->removeDockWidget(dock); dock->resize(dock->minimumSizeHint()); this->addDockWidget(Qt::TopDockWidgetArea , dock); dock->setVisible(true);}
else if (dockWidgetArea(dock) == Qt::LeftDockWidgetArea ) {leftArea. push_back(dock); this->removeDockWidget(dock); dock->resize(dock->minimumSizeHint()); this->addDockWidget(Qt::LeftDockWidgetArea , dock); dock->setVisible(true);}
else if (dockWidgetArea(dock) == Qt::RightDockWidgetArea ) {rightArea. push_back(dock); this->removeDockWidget(dock); dock->resize(dock->minimumSizeHint()); this->addDockWidget(Qt::RightDockWidgetArea , dock); dock->setVisible(true);}
else if (dockWidgetArea(dock) == Qt::BottomDockWidgetArea ) {bottomArea.push_back(dock); this->removeDockWidget(dock); dock->resize(dock->minimumSizeHint()); this->addDockWidget(Qt::BottomDockWidgetArea, dock); dock->setVisible(true);}
});
// then, tab them all
// definition: areas == top && bottom && etc..
// definition: area == top || bottom || etc...
for (QVector<QVector<QDockWidget*>*>::iterator areasItr = dockAreas.begin(); areasItr != dockAreas.end(); areasItr++)
{
// within each area, tab all the docks if there are more than 1
QVector<QDockWidget*> area = **areasItr;
auto areaRange = make_adjacent_range(area);
for (auto areaItr = areaRange.begin(); areaItr != areaRange.end(); ++areaItr)
{
auto dockPair = *areaItr;
this->tabifyDockWidget(dockPair.first, dockPair.second);
}
}
}
如果你使用像QVector这样的向量,而不是使用原始指针,那是行不通的?@danielfranca我认为最终是c++11编译器支持问题msvc2010。我认为这段代码将在支持基于范围的循环的较新版本上编译/运行。
error C2143: syntax error : missing ',' before ':'
error C2530: 'dockPair' : references must be initialized
error C3531: 'dockPair': a symbol whose type contains 'auto' must have an initializer
error C2143: syntax error : missing ';' before '{'
void mainWindow::tabifyDocks()
{
// get a list of all the docks
QList<QDockWidget*> docks = findChildren<QDockWidget*>();
// first, un-float all the tabs
std::for_each(docks.begin(), docks.end(), std::bind(&QDockWidget::setFloating, std::placeholders::_1 /* the dock widget*/, false));
// sort them into dockWidget areas
QVector<QDockWidget*> topArea, leftArea, rightArea, bottomArea;
QVector<QVector<QDockWidget*>*> dockAreas;
dockAreas.push_back(&topArea);
dockAreas.push_back(&leftArea);
dockAreas.push_back(&rightArea);
dockAreas.push_back(&bottomArea);
std::for_each(docks.begin(), docks.end(), [&] (QDockWidget* dock)
{
if (dockWidgetArea(dock) == Qt::TopDockWidgetArea ) {topArea. push_back(dock); this->removeDockWidget(dock); dock->resize(dock->minimumSizeHint()); this->addDockWidget(Qt::TopDockWidgetArea , dock); dock->setVisible(true);}
else if (dockWidgetArea(dock) == Qt::LeftDockWidgetArea ) {leftArea. push_back(dock); this->removeDockWidget(dock); dock->resize(dock->minimumSizeHint()); this->addDockWidget(Qt::LeftDockWidgetArea , dock); dock->setVisible(true);}
else if (dockWidgetArea(dock) == Qt::RightDockWidgetArea ) {rightArea. push_back(dock); this->removeDockWidget(dock); dock->resize(dock->minimumSizeHint()); this->addDockWidget(Qt::RightDockWidgetArea , dock); dock->setVisible(true);}
else if (dockWidgetArea(dock) == Qt::BottomDockWidgetArea ) {bottomArea.push_back(dock); this->removeDockWidget(dock); dock->resize(dock->minimumSizeHint()); this->addDockWidget(Qt::BottomDockWidgetArea, dock); dock->setVisible(true);}
});
// then, tab them all
// definition: areas == top && bottom && etc..
// definition: area == top || bottom || etc...
for (QVector<QVector<QDockWidget*>*>::iterator areasItr = dockAreas.begin(); areasItr != dockAreas.end(); areasItr++)
{
// within each area, tab all the docks if there are more than 1
QVector<QDockWidget*> area = **areasItr;
auto areaRange = make_adjacent_range(area);
for (auto areaItr = areaRange.begin(); areaItr != areaRange.end(); ++areaItr)
{
auto dockPair = *areaItr;
this->tabifyDockWidget(dockPair.first, dockPair.second);
}
}
}