- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我实现了一个单位文本框,它能够在选择不同单位时进行转换。这是通过使用 QHBoxLayout 将 QLineEdit 和 QLabel 粘在一起来完成的。
class LengthTextBox::Impl
{
public:
Impl() :
lblUnit(new QLabel()),
txtValue(new QLineEdit()),
menuLength(new LengthMenu(lblUnit)),
precision(2),
unit(units::Length::Meters)
{
}
QLabel* lblUnit;
QLineEdit* txtValue;
LengthMenu* menuLength;
int precision;
units::Length unit;
};
LengthTextBox::LengthTextBox(QWidget* parent) : QWidget(parent),
pimpl()
{
const auto hLayout = new QHBoxLayout(this);
// Adjust spacing for better fit within tables.
const auto spacing = 2;
auto margins = hLayout->contentsMargins();
margins.setTop(0);
margins.setLeft(spacing);
margins.setRight(spacing);
margins.setBottom(0);
hLayout->setContentsMargins(margins);
hLayout->setSpacing(spacing);
this->pimpl->txtValue->setValidator(new QDoubleValidator());
// Install an event filter to capture focus out events.
this->pimpl->txtValue->installEventFilter(this);
hLayout->addWidget(this->pimpl->txtValue);
hLayout->addWidget(this->pimpl->lblUnit);
// Since we're building on top of a QWidget, set up the focus policies to still focus on the text box.
this->setFocusProxy(this->pimpl->txtValue);
this->pimpl->txtValue->setFocusPolicy(Qt::StrongFocus);
this->pimpl->lblUnit->setFocusPolicy(Qt::ClickFocus);
// Connect signals for switching between units.
this->connect(this->pimpl->lblUnit, &QLabel::linkActivated, this, &LengthTextBox::unitSelectorClicked);
this->connect(this->pimpl->menuLength, &LengthMenu::unitChanged, this, &LengthTextBox::setUnit);
// Forward the line edit signals.
this->connect(this->pimpl->txtValue, &QLineEdit::editingFinished, this, &LengthTextBox::editingFinished);
this->connect(this->pimpl->txtValue, &QLineEdit::returnPressed, this, &LengthTextBox::returnPressed);
this->connect(this->pimpl->txtValue, &QLineEdit::textChanged, this, &LengthTextBox::textChanged);
this->connect(this->pimpl->txtValue, &QLineEdit::textEdited, this, &LengthTextBox::textEdited);
this->setUnit(this->pimpl->unit);
}
LengthTextBox::~LengthTextBox()
{
}
void LengthTextBox::setPrecision(int x)
{
this->pimpl->precision = x;
}
int LengthTextBox::getPrecision() const
{
return this->pimpl->precision;
}
void LengthTextBox::setUnit(const units::Length& x)
{
const auto value = this->getValue();
this->setValue(units::Convert(value, this->pimpl->unit, x));
// Use HTML to render the text as a link. This will lead to the linkActivated signal when clicked.
const QString link = "<b><a href='#'>" + QString::fromStdString(units::GetSymbol(x)) + "</a></b>";
this->pimpl->lblUnit->setText(link);
this->pimpl->txtValue->setFocus(Qt::FocusReason::OtherFocusReason);
this->pimpl->txtValue->selectAll();
this->pimpl->unit = x;
this->unitChanged(this->pimpl->unit);
}
units::Length LengthTextBox::getUnit() const
{
return this->pimpl->unit;
}
void LengthTextBox::setValue(double x, const units::Length& fromUnit)
{
if(fromUnit != units::Length::Unknown)
{
x = units::Convert(x, fromUnit, this->pimpl->unit);
}
this->pimpl->txtValue->setText(QString::number(x, 'f', this->pimpl->precision));
}
double LengthTextBox::getValue(const units::Length& toUnit) const
{
auto value = this->pimpl->txtValue->text().toDouble();
if(toUnit != units::Length::Unknown)
{
value = units::Convert(value, this->pimpl->unit, toUnit);
}
return value;
}
std::string LengthTextBox::getFormattedText() const
{
return this->pimpl->txtValue->text().toStdString() + " " + units::GetSymbol(this->pimpl->unit);
}
void LengthTextBox::unitSelectorClicked()
{
this->pimpl->menuLength->popup(QCursor::pos());
}
bool LengthTextBox::eventFilter(QObject* obj, QEvent* x)
{
// Watch for a focus out event on the text box inorder to send a lost focus signal.
if(this->pimpl->txtValue == obj)
{
if(x->type() == QEvent::FocusOut && this->pimpl->lblUnit->hasFocus() == false)
{
this->lostFocus();
return true;
}
}
return QWidget::eventFilter(obj, x);
}
接下来,我派生了一个 QStyledItemDelegate,这样我就可以在我自己的 QTableView 中使用这个文本框。
如果仔细观察,您会发现当编辑器在此委托(delegate)上处于事件状态时,字母“m”呈现在超链接“m”后面。
class LengthTextBoxDelegate::Impl
{
public:
Impl(int precision) :
txtLength(std::make_unique<LengthTextBox>())
{
this->txtLength->setPrecision(precision);
}
// By using a length text box to manage current selected unit, all conversion logic remains in one place.
std::unique_ptr<LengthTextBox> txtLength;
};
LengthTextBoxDelegate::LengthTextBoxDelegate(int precision, QWidget* parent) : QStyledItemDelegate(parent),
pimpl(precision)
{
}
LengthTextBoxDelegate::~LengthTextBoxDelegate()
{
}
QString LengthTextBoxDelegate::displayText(const QVariant& value, const QLocale&) const
{
this->pimpl->txtLength->setValue(value.toDouble(), Length::Meters);
return QString::fromStdString(this->pimpl->txtLength->getText()) + " " + QString::fromStdString(GetSymbol(this->pimpl->txtLength->getSelectedUnit()));
}
QWidget* LengthTextBoxDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem&, const QModelIndex& index) const
{
const auto txtLength = new LengthTextBox(parent);
txtLength->setValue(index.data().toDouble());
txtLength->setSelectedUnit(this->pimpl->txtLength->getSelectedUnit());
txtLength->setPrecision(this->pimpl->txtLength->getPrecision());
// AMS // This is kind of a hack. For some reason, the ::displayText() that is rendered when no editor is created, was getting rendered
// behind the editor widget and was appearing through as if the text box was transparent.
// By setting the background color white, we get a nice aesthetic that matches the table views that use this delegate.
// In addition, it covers up the text that was appearing behind the widget.
auto p = txtLength->palette();
p.setColor(QPalette::Base, Qt::white);
txtLength->setPalette(p);
//txtLength->setAutoFillBackground(true);
// Track the current unit so that its symbol can be rendered when ::displayText() is called.
this->connect(txtLength, &LengthTextBox::unitChanged, this, &LengthTextBoxDelegate::unitChanged);
this->connect(txtLength, &LengthTextBox::returnPressed, this, &LengthTextBoxDelegate::commitAndCloseEditor);
this->connect(txtLength, &LengthTextBox::lostFocus, this, &LengthTextBoxDelegate::commitAndCloseEditor);
return txtLength;
}
void LengthTextBoxDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const
{
const auto txtLength = qobject_cast<LengthTextBox*>(editor);
assert(txtLength != nullptr);
txtLength->setValue(index.data().toDouble(), Length::Meters);
}
void LengthTextBoxDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const
{
const auto txtLength = qobject_cast<LengthTextBox*>(editor);
assert(txtLength != nullptr);
model->setData(index, txtLength->getValue(Length::Meters));
}
void LengthTextBoxDelegate::unitChanged(const hive::math::units::Length& x)
{
this->pimpl->txtLength->setSelectedUnit(x);
}
void LengthTextBoxDelegate::commitAndCloseEditor()
{
const auto txtLength = qobject_cast<LengthTextBox*>(this->sender());
this->commitData(txtLength);
this->closeEditor(txtLength);
}
到目前为止,我想出的唯一解决方案是在我的编辑器小部件上设置背景颜色。这感觉很糟糕,一定是我做错了什么。
有没有办法在我的编辑器处于事件状态时阻止呈现 Qt::DisplayRole 文本?
最佳答案
您需要在绘制编辑器小部件之前填充背景。 QWidget
的 autoFillBackground
属性控制此行为,因为它默认设置为 false
,您的编辑器小部件将绘制在 QTableView
上。
您只需要在返回之前在 createEditor()
中设置编辑器的属性:
txtLength->setAutoFillBackground(true);
背景颜色使用 setBackgroundRole()
指定,它默认继承父级的背景(如果父级是 QTableView
,则为 QPalette::Base
)。因此,您将获得编辑器的白色背景,您可能需要指定 QPalette::Window
颜色角色:
txtLength->setBackgroundRole(QPalette::Window);
关于c++ - 编辑器处于事件状态时呈现的 Qt 委托(delegate)显示文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40997498/
我正在使用 Qt 语言学家翻译一个 ui 文件。我使用 lupdate 获取了它的 ts 文件,并翻译了这些单词和短语。现在我想将它添加到我的代码中,但我从它的教程中发现我似乎必须将 tr() 添加到
我想在 Qt Creator 中创建下面的简单控制台应用程序: #include int main(int argc, char* argv[]) { std::cout #include
我想将 libQtGui.so.4 libQtNetwork.so.4 和 libQtCore.so.4 包含在与我的应用程序所在的目录相同的目录中。我如何让 Qt 理解这一点? y 目的是拥有一个使
我有一个充满 QPushButtons 和 QLabels 以及各种其他有趣的 QWidget 的窗口,所有这些都使用各种 QLayout 对象动态布局...而我想做的是偶尔制作一些这些小部件变得不可
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎与 help center 中定义的范围内的编程无关。 . 关闭 7 年前。 Improve
我想知道 Qt 是否将下面代码的“版本 1”之类的东西放在堆上?在版本 1 中,Qt 会将 dirStuff 放在堆栈上还是堆上?我问是因为我有一种感觉,Java 将所有数据结构放在堆上......不
这个问题是关于 Qt Installer Framework 2.0 版的。 在这一点上,使用 Qt 安装程序框架的人都知道,如果不进行自定义,您根本无法通过安装程序覆盖现有安装。这样做显然是为了解决
关闭。这个问题是off-topic .它目前不接受答案。 想改善这个问题吗? Update the question所以它是 on-topic对于堆栈溢出。 8年前关闭。 Improve this q
因为我在我的计算机上安装了 Qt 4.8.4 和 Qt 5.1,所以我遇到了问题。 当只有 Qt 4.8.4 存在时,一切都很好。 当我添加 Qt 5.1 时,这个工作正常,但 Qt 4.8.4 给了
我无法在我的 Ubuntu 12 中安装更多软件包。我尝试了 apt-get install -f ,以及许多其他类似的技巧,但在找到解决方案方面没有进展。 这是属于 Qt 的损坏包: 以下包具有未满
我正在尝试使用 Virtual Box 中的 Ubuntu 机器复制我们目前在物理 Ubuntu 服务器上运行的应用程序。它是一个 QT 应用程序,但在服务器上我们使用 NPM 的 pm2 运行它。安
问题: Qt Creator 是用 Qt Creator 构建的吗? 同样,Qt Designer 是用 Qt Designer 构建的吗? 顺便说一句,为什么有两个 Qt IDE?他们是竞争对手吗?
当我使用 QWidget设计用户界面时,我总是对它的大小属性有点困惑。有size policy , geometry和 hintSize . 我只知道size policy之间的关系和 hintSiz
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我想知道是否有一种很好的方法可以让用户像 LabView 一样创建节点图(有限制)。 像这样的东西: 我见过http://www.pyqtgraph.org/ ,这似乎有类似的东西,我确实打算使用 P
在 Qt 中是否有一种跨平台的方式来获得用户喜欢的固定宽度和比例字体? 例如,在 cocoa 中,有 NSFont *proportional = [NSFont userFontOfSize:12.
我想使用 Qt 和 C++ 制作这样的交互式图表:http://jsxgraph.uni-bayreuth.de/wiki/index.php/Cubic_spline_interpolation 关
我正在编写一个嵌入式设备屏幕的模拟(其中包含主 QWidget 顶部的自定义小部件),虽然屏幕的原始尺寸是 800x600,但我希望能够按比例放大和缩小它拖动窗口的角。如果不使用网格布局和担架(不会向
在下面的示例中,我是否必须从堆中删除对象?如果是的话,怎么办? #include #include #include #include #include int main(int argc,
来自 Web 开发背景,我现在进入 QT 应用程序开发。 使用 QFonts 我已经看到我显然只有两个选择,在 QT 中定义字体大小;按像素大小或点大小。 在制作网页布局时,我习惯于以相对方式定义所有
我是一名优秀的程序员,十分优秀!