I have a simple app with text edit - QPlainTextEdit, created by qt designer. I just need from mainwindow.cpp setViewportMargins.But I get the following error message - void QAbstractScrollArea::setViewportMargins(int, int, int, int)' is protected.
Code:
ui->textEdit->setViewportMargins(10,10,10,10);
Is there any way to do this?
Protected functions can only be used within the class, so it generates the error. I have solved it by designing the CustomPlainText
class and promoting it to the original QPlainTextEdit
, inside it I have used setViewportMargins (10,10,10,10);
. If you want to access that function from outside the class I have created the functions CustomSetViewportMargins({args})
.
Right click and select: Promote to..
Write the name of the class and the header and add it.
And press promote.
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);
}
Before:
After:
Complete Code: https://github.com/eyllanesc/stackoverflow/tree/master/CustomPlainTextProject