powerbuilder 通过点击按钮设置数据窗口中的某一列下拉框不可编辑
时间: 2023-08-24 19:05:15 浏览: 39
在 PowerBuilder 中,可以通过以下步骤来设置数据窗口中的某一列下拉框不可编辑:
1. 打开窗口编辑器,选择要设置的数据窗口控件。
2. 在属性窗口中选择“DataWindow”选项卡。
3. 选择要设置的列,在“Edit Style”属性中选择“Dropdown List”或“Dropdwon Calendar”。
4. 在窗口的代码中找到按钮的点击事件处理函数。
5. 在事件处理函数中,使用以下代码将列的下拉框设置为不可编辑:
dw_1.Object.column.DroppedDown = False
dw_1.Object.column.Enabled = False
其中,“dw_1”是数据窗口控件的名称,“object.column”是要设置的列的名称。这样设置后,该列的下拉框将不可编辑,同时该列的文本框也将不可用。如果只想设置该列下拉框不可用,而不是不可编辑,可以只使用以上代码的最后一行,将“Enabled”属性设置为“False”。
相关问题
QTTablewidget 某一列设置为下拉框
可以使用`QComboBox`控件作为`QTableWidget`中某一列的单元格的编辑器。具体实现方法如下:
1. 创建一个`QComboBox`对象,并将其添加到需要添加下拉框的列中的单元格中,例如:
```cpp
QComboBox *comboBox = new QComboBox();
comboBox->addItem("Option 1");
comboBox->addItem("Option 2");
comboBox->addItem("Option 3");
tableWidget->setCellWidget(row, col, comboBox);
```
这里的`tableWidget`是`QTableWidget`对象,`row`和`col`表示需要添加下拉框的单元格所在的行和列。
2. 为`QComboBox`对象添加选项,例如:
```cpp
comboBox->addItem("Option 1");
comboBox->addItem("Option 2");
comboBox->addItem("Option 3");
```
3. 为`QTableWidget`对象设置委托,用于实现下拉框的显示和编辑,例如:
```cpp
tableWidget->setItemDelegateForColumn(col, new QComboBoxDelegate());
```
这里的`col`表示需要添加下拉框的列的索引,`QComboBoxDelegate`是自定义的委托类,继承自`QItemDelegate`,并重写了`createEditor()`、`setEditorData()`和`setModelData()`函数,用于创建和设置下拉框编辑器。
```cpp
class QComboBoxDelegate : public QItemDelegate
{
public:
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override
{
QComboBox *editor = new QComboBox(parent);
editor->addItem("Option 1");
editor->addItem("Option 2");
editor->addItem("Option 3");
return editor;
}
void setEditorData(QWidget *editor, const QModelIndex &index) const override
{
QComboBox *comboBox = qobject_cast<QComboBox *>(editor);
comboBox->setCurrentIndex(comboBox->findText(index.data(Qt::EditRole).toString()));
}
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override
{
QComboBox *comboBox = qobject_cast<QComboBox *>(editor);
model->setData(index, comboBox->currentText(), Qt::EditRole);
}
};
```
在委托类中,`createEditor()`函数用于创建下拉框编辑器,`setEditorData()`函数用于设置下拉框编辑器的初始值,`setModelData()`函数用于将下拉框编辑器的值设置回数据模型中。
QTTablewidget 某一列设置为下拉框并设置默认值
要将QTTableWidget中的某一列设置为下拉框并设置默认值,可以通过自定义QComboBox委托实现。下面是一个示例代码:
```python
from PyQt5.QtWidgets import QComboBox, QTableWidgetItem, QTableWidget, QApplication, QWidget, QHBoxLayout, QTableWidgetItem, QStyledItemDelegate
class ComboBoxDelegate(QStyledItemDelegate):
def __init__(self, choices=[], parent=None):
super().__init__(parent)
self.items = choices
def createEditor(self, parent, option, index):
editor = QComboBox(parent)
editor.addItems(self.items)
return editor
def setEditorData(self, editor, index):
value = index.data()
editor.setCurrentIndex(editor.findText(value))
def setModelData(self, editor, model, index):
value = editor.currentText()
model.setData(index, value, 0)
class MyTableWidget(QTableWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.setColumnCount(2)
self.setHorizontalHeaderLabels(['Name', 'Gender'])
self.setItemDelegateForColumn(1, ComboBoxDelegate(["Male", "Female"], self))
self.setRowCount(1)
self.setItem(0, 0, QTableWidgetItem("Tom"))
self.setItem(0, 1, QTableWidgetItem("Male"))
if __name__ == '__main__':
app = QApplication([])
widget = MyTableWidget()
widget.show()
app.exec_()
```
在上述代码中,我们首先定义了一个自定义的QStyledItemDelegate,它继承自QStyledItemDelegate,并重写了createEditor、setEditorData和setModelData方法,分别用于创建下拉框、设置下拉框的默认值和将下拉框的值更新到数据模型中。
然后,在MyTableWidget中,我们将第二列设置为ComboBoxDelegate,并将其选项设置为["Male", "Female"],这样就将第二列设置为了下拉框,并且默认值为"Male"。