gpt4 book ai didi

c++ - QPlainTextEdit - setViewportMargins, protected

转载 作者:行者123 更新时间:2023-11-28 05:20:35 29 4
gpt4 key购买 nike

我有一个简单的文本编辑应用程序 - QPlainTextEdit,由 qt designer 创建。我只需要从 mainwindow.cpp setViewportMargins。但是我收到以下错误消息 - void QAbstractScrollArea::setViewportMargins(int, int, int, int)' is protected.

代码:

ui->textEdit->setViewportMargins(10,10,10,10);

有什么办法吗?

最佳答案

protected 函数只能在类中使用,所以会产生错误。我已经通过设计 CustomPlainText 类并将其提升为原始 QPlainTextEdit 解决了这个问题,在其中我使用了 setViewportMargins (10,10,10,10); 。如果您想从类外部访问该函数,我创建了函数 CustomSetViewportMargins({args})

右键单击并选择:Promote to..

enter image description here

写好类名和header,然后添加。

enter image description here

然后按推广。

enter image description here

customplaintext.cpp

#ifndef CUSTOMPLAINTEXT_H
#define CUSTOMPLAINTEXT_H

#include <QPlainTextEdit>

class CustomPlainText:public QPlainTextEdit
{
Q_OBJECT
public:
CustomPlainText(QWidget * parent = 0);
void CustomSetViewportMargins(int left, int top, int right, int bottom);
void CustomSetViewportMargins(const QMargins & margins);
};

#endif // CUSTOMPLAINTEXT_H

customplaintext.h

#include "customplaintext.h"

CustomPlainText::CustomPlainText(QWidget *parent): QPlainTextEdit(parent)
{
setViewportMargins(10,10,10,10);
}

void CustomPlainText::CustomSetViewportMargins(int left, int top, int right, int bottom)
{
setViewportMargins(left, top, right, bottom);
}

void CustomPlainText::CustomSetViewportMargins(const QMargins & margins)
{
setViewportMargins(margins);
}

之前:

enter image description here

之后:

enter image description here

完整代码: https://github.com/eyllanesc/stackoverflow/tree/master/CustomPlainTextProject

关于c++ - QPlainTextEdit - setViewportMargins, protected ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41615743/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com