我正在尝试删除QLabel(子类为Label)中的文本。问题在于自定义项paintEvent()不遵守样式表设置的变量(尤其是填充/边距)。我该如何解决?
如您所见,我有一个解决方案,大多数情况下都是正确的,但是边距不能正常运行。另一个解决方案远没有奏效,而是走了正确的道路。我知道我需要做一些事情QStyle来获取区域的大小,但是我不明白。该文档确实令人困惑。
void Label::paintEvent(QPaintEvent *event){
if (this->elide == Qt::ElideNone){
QLabel::paintEvent(event);
return;
}
QPainter p(this);
QFontMetrics fm(font());
if (fm.width(text()) > contentsRect().width()) {
if (1){ //this kind of works...
QRect rect = this->contentsRect();
rect.adjust(this->margin(), this->margin(), -this->margin(), -this->margin());
QString elided_txt = this->fontMetrics().elidedText(text(), this->elide, rect.width(), Qt::TextShowMnemonic); //This is the key line.
p.drawText(rect(), elided_txt, QTextOption(Qt::AlignVCenter | Qt::AlignLeft));
} else { //the correct solution should look something like this:
QStyle *style = this->style();
QRect rect = style->itemTextRect(fm, this->rect(), Qt::AlignVCenter | …Run Code Online (Sandbox Code Playgroud) 我想迭代树模型中的所有索引,如图所示。
我编写的函数给出了堆栈溢出错误。
void iterate(const QModelIndex & index, const QAbstractItemModel * model)
{
if (index.isValid())
PrintData( index );
if (!model->hasChildren(index) || (index.flags() & Qt::ItemNeverHasChildren))
{
return;
}
auto rows = model->rowCount();
for (int i = 0; i < rows; ++i)
iterate(model->index(i, 0, index), model);
}
Run Code Online (Sandbox Code Playgroud)
我想要QLineEdit不显示输入的文本,而是显示经过处理的版本,同时保留原始文本并在通过 请求时返回它text()。就像密码回显模式一样,但我不希望每个字符都被屏蔽。我想虚拟化空间:
例如 当some text with spaces in between输入时,some\xc2\xb7text\xc2\xb7with\xc2\xb7spaces\xc2\xb7in\xc2\xb7between应显示出来以便人们可以看到空格。就像您在 LibreOffice 中激活 \xc2\xb6 符号一样。
有QLineEdit::displayText(),但是不能设置,只能读取。另外,echoMode只能通过枚举来设置,并且通过EchoMode::Password设置,处理似乎发生在 的私有函数中QLineEdit,因此我也无法覆盖某些处理函数。
这可能吗?
\n我正在尝试将自定义对象复制并粘贴QListView到同一应用程序的另一个窗口中,但是此类仅返回一个std::ostream,并且我无权修改此类。
我怎样才能把这个的std :: ostream的中QDatastream再QClipboard进行复制和粘贴的过程?
void ProjectModel::copymodel(QModelIndex idx){
const mo::model model(modelList.at(idx.row()));
std::ostream *stream;
stream << &model;
QByteArray itemData;
QDataStream datastream(&itemData, QIODevice::WriteOnly);
QMimeData *mimeData = new QMimeData;
mimeData->setData("test", itemData);
QApplication::clipboard()->setMimeData(mimeData);}
Run Code Online (Sandbox Code Playgroud)
自定义类mo::model返回std::stream如下:
namespace mo {
::std::ostream& operator<< (::std::ostream& o, const model& i){
o << ::std::endl << "name: " << i.name ();
if (i.box ())
{
o << ::std::endl << "box: " << *i.box ();
}
for (model::basic_element_const_iterator
b (i.basic_element ().begin ()), …Run Code Online (Sandbox Code Playgroud) 我使用此代码检查了Arduino引脚8的状态。查看该引脚是高电平还是低电平,但是我的输出从高电平连续变为低电平。
运行此代码时,我没有将任何东西连接到引脚8。
const int Pin = 8;
int Reading=0;
void setup() {
Serial.begin(9600);
delay(2000);
pinMode(Pin, INPUT);
}
void loop() {
Reading = digitalRead(Pin);
if(Reading == HIGH)
{
Serial.println("HIGH");
delay(2000);
}
if(Reading == LOW)
{
Serial.println("LOW");
delay(2000);
}
}
Run Code Online (Sandbox Code Playgroud)
但是我的输出是这样的:输出:
HIGH
HIGH
LOW
LOW
HIGH
HIGH
LOW
LOW
HIGH
HIGH
LOW
LOW
HIGH
HIGH
LOW
LOW
Run Code Online (Sandbox Code Playgroud)
不知道该怎么办??