Perhaps, this could help:
http://stackoverflow.com/questions/3...-a-qvboxlayout
Perhaps, this could help:
http://stackoverflow.com/questions/3...-a-qvboxlayout
Thanks for reply, but I tried this (first code block of my first post).
When I hide ScrollBar it looks like I want, when it is visible is what destroys my idea - the ScrollBar because it's almost in the middle of layout.
Now I try to do this by using custom scrollbar: I hide the default scrollbar, set margins using StyleSheet, and create new scrollbar and insert it on the right side of the layoutMain (out of textEdit).
But I have problem with setting position of new scrollbar. When I use:Qt Code:
textEdit->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); textEdit->setStyleSheet("QTextEdit" "{margin-left: 40px;" "margin-right: 40px;" "margin-top: 20px;" "margin-bottom: 20px}");To copy to clipboard, switch view to plain text mode
There is small ScrollBar on the left side, which doesn't work.Qt Code:
scrollBar->setStyleSheet("background-color: gray;"); scrollBar->show();To copy to clipboard, switch view to plain text mode
Anyone know how to put it on the right, alongside textEdit (by layoutMain edge)?
This approach works here:
Qt Code:
#include <QtGui> Q_OBJECT public: setViewportMargins(30, 30, 30, 30); // margins are in pixels setStyleSheet("TextEdit { background: white; }"); } }; int main(int argc, char **argv) { // long text omitted ); TextEdit e; e.setText(text); e.show(); return app.exec(); } #include "main.moc"To copy to clipboard, switch view to plain text mode
An alternate would be to retrieve the QTextDocument from the editor and setPageSize() to match the viewport size and setPageMargins() to give the space around. You would have to do it each time the viewport resizes and possibly convert between screen pixels and text document points. This would have a different effect when scrolling though.
Acamapichtli (9th April 2014), rawfool (8th April 2014)
Thank you for reply. However I have one problem using this method.
I created a following class testdocument.h, as you showed me:
Then I try to put it onto layout (TestApp.cpp):Qt Code:
#include <QtGui> #include <QTextEdit> { Q_OBJECT public: { setViewportMargins(30,30,30,30); } };To copy to clipboard, switch view to plain text mode
When I want to compile I get just one error:Qt Code:
#include <TestApp.h> #include <testdocument.h> TestApp::TestApp() { //textEdit = new QTextEdit; TestDocument doc; layoutMain->addWidget(doc); layoutMain->setMargin(0); window->setLayout(layoutMain); setCentralWidget(window); }To copy to clipboard, switch view to plain text mode
And I don't know what I've done worng. No errors about new class, TestDocument is widget, can you point me what is wrong with this?no matching function for call to 'QVBoxLayout::addWidget(TestDocument&)'
layoutMain->addWidget(doc);
Yes, but it should be a pointer to widget. Allocate it on the heap using "operator new" just like "window" in line 13.TestDocument is widget, can you point me what is wrong with this?
Acamapichtli (9th April 2014)
The QWidget parameter must be a pointer
Qt Code:
layoutMain->addWidget(&doc);To copy to clipboard, switch view to plain text mode
Acamapichtli (9th April 2014)
"doc" is gonna go out of scope very soonQt Code:
layoutMain->addWidget(&doc);To copy to clipboard, switch view to plain text mode
Acamapichtli (9th April 2014)
Thank you all for your contribution, now code will finally compile without errors, but unfornately doc (new TestDocument widget) won't show up.
This is all the code I have for this app:
TestApp.h
TestDocument.hQt Code:
#ifndef TESTAPP_H #define TESTAPP_H #include <QMainWindow> #include <QVBoxLayout> #include <QScrollBar> #include <QAbstractScrollArea> { Q_OBJECT public: TestApp(); private: QVBoxLayout *layoutMain; QWidget *window; QScrollBar *scrollBar; QAbstractScrollArea *scrollArea; }; #endif // TESTAPP_HTo copy to clipboard, switch view to plain text mode
TestApp.cppQt Code:
#ifndef TESTDOCUMENT_H #define TESTDOCUMENT_H #include <QtGui> #include <QTextEdit> { Q_OBJECT public: { setViewportMargins(30,30,30,30); } }; #endif // TESTDOCUMENT_HTo copy to clipboard, switch view to plain text mode
main.cppQt Code:
#include <TestApp.h> #include <TestDocument.h> #include <QtGui> TestApp::TestApp() { TestDocument doc; layoutMain->addWidget(&doc); //layoutMain->setMargin(0); window->setLayout(layoutMain); setCentralWidget(window); }To copy to clipboard, switch view to plain text mode
First I thought it was too big margins, but even if I set them all to 10, doc won't show up.Qt Code:
#include <QApplication> #include <TestApp.h> int main(int argc, char *argv[]) { TestApp test; test.show(); return app.exec(); }To copy to clipboard, switch view to plain text mode
I tried using doc.show(), I tried declaring "TestDocument *doc;" in TestApp.h and then "doc = new TestDocument" in TestApp.cpp, but neither works (second method showed some errors, so it had to be wrong idea).
because it goes out of scope and is deleted.doc won't show up.
Why in TestApp.h ? Just change the declaration of doc in constructor:TestDocument *doc;" in TestApp.h and then "doc = new TestDocument" in TestApp.cpp, but neither works (second method showed some errors, so it had to be wrong idea
Qt Code:
TestApp::TestApp() { TestDocument * doc = new TestDocument; layoutMain->addWidget(doc); //layoutMain->setMargin(0); window->setLayout(layoutMain); setCentralWidget(window); }To copy to clipboard, switch view to plain text modeusually those errors will tell you why it is a "wrong idea"...(second method showed some errors, so it had to be wrong idea)
Acamapichtli (9th April 2014)
Now I see.
Sorry if I bored someone with my clumsy trying to achieve what I wanted, I'm just a newbie and I don't know some basic things about programming
With your help it works now!
Thank you all for your help.
Last edited by Acamapichtli; 9th April 2014 at 17:50.
Bookmarks