QT以编程方式制作下拉菜单小部件
QT Making a Dropdown Menu Widget Programmatically
我在 Mac OSX 中使用 QT 5.5。我想以编程方式制作几个下拉菜单小部件,这些小部件将具有可以更改某些变量值的各种选项。
例如,我会让下拉菜单 1 代表变量 "command" 有:
- 问
-W
-E
-R
通过选择任何一个,然后它会使 command = Q 或 command = W。这样,我可以向另一个知道我发送了 Q 或 W 的程序发送命令。
我当前的主窗口如下所示:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
//******* Set up
ui->setupUi(this);
ui->centralWidget->setLayout(new QVBoxLayout);
// 01: Creation of Console
console = new Console;
console->setEnabled(false);
/************** Adding Widgets *********************/
//creation and attribution of slider
slider = new QSlider();
slider->resize(255, 20);
slider->setOrientation(Qt::Horizontal);
slider->setRange(0, 255); //0-255 is range we can read
//creation and attribution of the lcd
lcd = new QLCDNumber();
lcd->setSegmentStyle(QLCDNumber::Flat);
lcd->resize(255, 50);
//03: Adding widgets to layout
//add console as a widget to the main widget
//layout with slider and lcd underneath console
ui->centralWidget->layout()->addWidget(console);
ui->centralWidget->layout()->addWidget(slider);
ui->centralWidget->layout()->addWidget(lcd);
////////I WANT TO ADD VARIOUS DROPDOWN MENUS HERE NEXT TO EACH OTHER////////
/************** Connection Events ***********************/
....
}
假设你想要一个 ComboBox,你可以这样做:
QStringList commands = { "Q", "W", "E", "R" };
QComboBox* combo = new QComboBox(this);
combo->addItems(commands);
connect( combo, &QComboBox::currentTextChanged, this, &MainWindow::commandChanged);
现在您将在用户更改组合框项目时获得命令文本。然后你可以根据那个写你的代码。
MainWindow::commandChanged(const QString& command_text)
{
//Do the logic based on command_text
}
如果您想选择不同的组合框项目文本,另一个选项是您设置组合框的 itemData
for combobox items. and get them in your slot by currentData
属性。
我在 Mac OSX 中使用 QT 5.5。我想以编程方式制作几个下拉菜单小部件,这些小部件将具有可以更改某些变量值的各种选项。
例如,我会让下拉菜单 1 代表变量 "command" 有: - 问 -W -E -R 通过选择任何一个,然后它会使 command = Q 或 command = W。这样,我可以向另一个知道我发送了 Q 或 W 的程序发送命令。
我当前的主窗口如下所示:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
//******* Set up
ui->setupUi(this);
ui->centralWidget->setLayout(new QVBoxLayout);
// 01: Creation of Console
console = new Console;
console->setEnabled(false);
/************** Adding Widgets *********************/
//creation and attribution of slider
slider = new QSlider();
slider->resize(255, 20);
slider->setOrientation(Qt::Horizontal);
slider->setRange(0, 255); //0-255 is range we can read
//creation and attribution of the lcd
lcd = new QLCDNumber();
lcd->setSegmentStyle(QLCDNumber::Flat);
lcd->resize(255, 50);
//03: Adding widgets to layout
//add console as a widget to the main widget
//layout with slider and lcd underneath console
ui->centralWidget->layout()->addWidget(console);
ui->centralWidget->layout()->addWidget(slider);
ui->centralWidget->layout()->addWidget(lcd);
////////I WANT TO ADD VARIOUS DROPDOWN MENUS HERE NEXT TO EACH OTHER////////
/************** Connection Events ***********************/
....
}
假设你想要一个 ComboBox,你可以这样做:
QStringList commands = { "Q", "W", "E", "R" };
QComboBox* combo = new QComboBox(this);
combo->addItems(commands);
connect( combo, &QComboBox::currentTextChanged, this, &MainWindow::commandChanged);
现在您将在用户更改组合框项目时获得命令文本。然后你可以根据那个写你的代码。
MainWindow::commandChanged(const QString& command_text)
{
//Do the logic based on command_text
}
如果您想选择不同的组合框项目文本,另一个选项是您设置组合框的 itemData
for combobox items. and get them in your slot by currentData
属性。