跟进this回答,我可以将Qt应用程序中的对象声明为:
QLabel *label {new QLabel("bla")};
或
QLabel label {QLabel("bla")};
让我们假设应用程序足够大;如果我将它们声明为第二种方式,即使它意味着它更快,那么堆栈的风险是否会因为Qt对象(以及程序的其余部分)而过于“拥挤”?如果是这样,我怎么知道什么时候避开第二种方法?
答案 0 :(得分:2)
你无法得到一个明确的答案:“在堆上还是堆栈上分配更好?”,这就是你要问的问题。
就像在评论中所说的那样,不应该在堆栈上分配大对象,但没有明确的限制,这将取决于许多事情,如堆栈大小或程序将运行的硬件。
另外我想指出,如果你可以确定将在堆上分配QLabel *label {new QLabel("bla")};
(除非你重新实现了new
运算符),你就不能说QLabel label {QLabel("bla")}
将被分配。
现在你在谈论Qt。设计Qt和QObject
哲学的方法,首选带有QObject
派生类指针的语法。但是没有理由反对使用其他形式。
另外因为Qt使用d指针隐藏其实现细节,Qt提供的QObject
派生类不应该非常大。例如,即使你在堆栈上分配QLabel
( 24个字节)它的大部分内部都将在堆上分配(404字节)。
答案 1 :(得分:1)
由于Sub SortMain()
'
' SortMain Macro
' Sortingandcoloring
'
'
Sheets("Key Performance Audience Metric").Select
Range("B5:H5").Select
Selection.AutoFilter
ActiveWorkbook.Worksheets("Key Performance Audience Metric").AutoFilter.Sort. _
SortFields.Clear
ActiveWorkbook.Worksheets("Key Performance Audience Metric").AutoFilter.Sort. _
SortFields.Add Key:=Range("C5:C55"), SortOn:=xlSortOnValues, Order:= _
xlDescending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Key Performance Audience Metric").AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Selection.AutoFilter
Range("K5:O5").Select
Selection.AutoFilter
ActiveWorkbook.Worksheets("Key Performance Audience Metric").AutoFilter.Sort. _
SortFields.Clear
ActiveWorkbook.Worksheets("Key Performance Audience Metric").AutoFilter.Sort. _
SortFields.Add Key:=Range("L5:L55"), SortOn:=xlSortOnValues, Order:= _
xlDescending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Key Performance Audience Metric").AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Selection.AutoFilter
Range("R5:W5").Select
Selection.AutoFilter
ActiveWorkbook.Worksheets("Key Performance Audience Metric").AutoFilter.Sort. _
SortFields.Clear
ActiveWorkbook.Worksheets("Key Performance Audience Metric").AutoFilter.Sort. _
SortFields.Add Key:=Range("S5:S55"), SortOn:=xlSortOnValues, Order:= _
xlDescending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Key Performance Audience Metric").AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Selection.AutoFilter
Range("A1").Select
Sheets("Engagement Quality Metrics").Select
Range("B5:L5").Select
Selection.AutoFilter
ActiveWorkbook.Worksheets("Engagement Quality Metrics").AutoFilter.Sort. _
SortFields.Clear
ActiveWorkbook.Worksheets("Engagement Quality Metrics").AutoFilter.Sort. _
SortFields.Add Key:=Range("C5:C54"), SortOn:=xlSortOnValues, Order:= _
xlDescending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Engagement Quality Metrics").AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Selection.AutoFilter
Range("O5:W5").Select
Selection.AutoFilter
ActiveWorkbook.Worksheets("Engagement Quality Metrics").AutoFilter.Sort. _
SortFields.Clear
ActiveWorkbook.Worksheets("Engagement Quality Metrics").AutoFilter.Sort. _
SortFields.Add Key:=Range("P5:P54"), SortOn:=xlSortOnValues, Order:= _
xlDescending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Engagement Quality Metrics").AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Selection.AutoFilter
Range("Z5:AH5").Select
Selection.AutoFilter
ActiveWorkbook.Worksheets("Engagement Quality Metrics").AutoFilter.Sort. _
SortFields.Clear
ActiveWorkbook.Worksheets("Engagement Quality Metrics").AutoFilter.Sort. _
SortFields.Add Key:=Range("AA5:AA54"), SortOn:=xlSortOnValues, Order:= _
xlDescending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Engagement Quality Metrics").AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
Selection.AutoFilter
End With
Sheets("Video Views").Select
Range("B5:D5").Select
Selection.AutoFilter
ActiveWorkbook.Worksheets("Video Views").AutoFilter.Sort. _
SortFields.Clear
ActiveWorkbook.Worksheets("Video Views").AutoFilter.Sort. _
SortFields.Add Key:=Range("C5:C55"), SortOn:=xlSortOnValues, Order:= _
xlDescending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Video Views").AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Selection.AutoFilter
Range("H5:J5").Select
Selection.AutoFilter
ActiveWorkbook.Worksheets("Video Views").AutoFilter.Sort. _
SortFields.Clear
ActiveWorkbook.Worksheets("Video Views").AutoFilter.Sort. _
SortFields.Add Key:=Range("I5:I55"), SortOn:=xlSortOnValues, Order:= _
xlDescending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Video Views").AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Sheets("Video Views").Select
Range("B5:D6").Select
Selection.Copy
Range("B5:D55").Select
Selection.PasteSpecial Paste:=xlFormats, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
Application.CutCopyMode = False
Range("H5:J6").Select
Selection.Copy
Range("H5:J55").Select
Selection.PasteSpecial Paste:=xlFormats, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
Sheets("Key Performance Audience Metric").Select
Range("B5:H6").Select
Selection.Copy
Range("B5:H55").Select
Selection.PasteSpecial Paste:=xlFormats, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
Application.CutCopyMode = False
Range("K5:O6").Select
Selection.Copy
Range("K5:O55").Select
Selection.PasteSpecial Paste:=xlFormats, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
Application.CutCopyMode = False
Range("R5:W6").Select
Selection.Copy
Range("R5:W55").Select
Selection.PasteSpecial Paste:=xlFormats, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
Application.CutCopyMode = False
Sheets("Key Performance Audience Metric").Select
Range("A1").Select
Sheets("Engagement Quality Metrics").Select
Range("B5:L6").Select
Selection.Copy
Range("B5:L54").Select
Selection.PasteSpecial Paste:=xlFormats, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
Application.CutCopyMode = False
Range("O5:W6").Select
Selection.Copy
Range("O5:W54").Select
Selection.PasteSpecial Paste:=xlFormats, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
Application.CutCopyMode = False
Range("Z5:AH6").Select
Selection.Copy
Range("Z5:AH54").Select
Selection.PasteSpecial Paste:=xlFormats, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
Application.CutCopyMode = False
Range("A1").Select
Sheets("MACROS").Select
Sheets("Cross Platform Table").Select
Range("A1").Select
Sheets("Circle Charts").Select
Dim i As Long
i = Application.Intersect(Range("A:A"), ActiveSheet.UsedRange).End(xlDown).Row
Do While Range("A1").Offset(i, 0).Value = 0
Range("A1").Offset(i, 0).EntireRow.Delete xlShiftUp
i = i - 1
Loop
Sheets("Key Performance Audience Metric").Select
Dim j As Long
j = Application.Intersect(Range("V:V"), ActiveSheet.UsedRange).End(xlDown).Row
Do While Range("V1").Offset(j, 0).Value = 0
Range("V1").Offset(j, 0).EntireRow.Delete xlShiftUp
j = j - 1
Loop
Sheets("Engagement Quality Metrics").Select
Dim k As Long
k = Application.Intersect(Range("AJ:AJ"), ActiveSheet.UsedRange).End(xlDown).Row
Do While Range("AJ1").Offset(k, 0).Value = 0
Range("AJ1").Offset(k, 0).EntireRow.Delete xlShiftUp
k = k - 1
Loop
Sheets("MACROS").Select
Range("C8:D12").Select
With Selection.Font
.color = -11489280
.TintAndShade = 0
End With
With Selection.Font
.ThemeColor = xlThemeColorLight1
.TintAndShade = 0
End With
ExecuteExcel4Macro "PATTERNS(1,0,5287936,TRUE,2,3,0,0)"
ActiveCell.FormulaR1C1 = "DONE! Ready to Use!"
Range("A1").Select
End Sub
不可复制,因此您提供的第二种方式无效,因此代码无法编译。也许你的意思是
QLabel
此外,它与堆栈完全没有关系。你可以在一个类中编写该行:
QLabel label{"bla"};
通常,你应该这样做。您当然可以将窗口小部件分配为自动变量 - 通常这只会在class MyUi : public QWidget {
QGridLayout m_layout{this};
QLabel m_label{"bla"};
};
内发生:
main
在这里你需要使用一些常识。这堂课到底有多大?请注意,从int main(int argc, char **argv) {
QApplication app{argc, argv};
...
MyUi ui;
ui.show();
return app.exec();
}
派生的所有Qt类都使用PIMPL并且是指针的大小,或者是几个指针的大小 - 因此非常小。
在大多数情况下,你不应该担心它。如果您的项目变得非常庞大时用完了堆栈,请动态分配QObject
,这就是全部:
MyUi
我个人从未遇到过这个问题:如果你的int main(int argc, char **argv) {
QApplication app{argc, argv};
...
auto ui = std::make_unique<MyUi>();
ui->show();
return app.exec();
}
课程足够大,你的设计可能会遇到严重的问题,这个课程太单一了而且责任太重了。