В настоящее время я пытаюсь закодировать двусторонний виджет Qt, отображающий тот же виджет "содержимое". Я имею в виду, что виджет должен быть похож на QScrollArea:
- Где левая сторона отображает первую часть (или всю часть) виджета «контент»
- Где правый сторона отображает оставшуюся (если есть) часть виджета «контент»
Вот (дрянной) пример кода, чтобы объяснить, что я имею в виду:
#include <QApplication>
#include <QLabel>
#include <QWebEngineView>
#include <QWidget>
#include <QHBoxLayout>
#include <QScrollBar>
#include <QScrollArea>
#include <QPainter>
class Widget : public QWidget
{
private:
int margin = 4;
QWidget* contentWidget = nullptr;
protected:
void resizeEvent(QResizeEvent* event)
{
contentWidget->setFixedWidth((width()-margin)/2);
contentWidget->setFixedHeight(height()*2);
}
void paintEvent(QPaintEvent* event)
{
if(contentWidget)
{
int w = contentWidget->width();
int h = height();
int y = h;
QPainter p(this);
p.setPen(Qt::NoPen);
p.setBrush(Qt::black);
contentWidget->render(&p, QPoint(0, 0), QRegion(0, 0, w, h));
contentWidget->render(&p, QPoint(w+margin, 0), QRegion(0, y, w, h));
p.drawRect(w, 0, margin, h);
}
}
public:
Widget(QWidget *parent = nullptr) : QWidget(parent) { }
void setContentWidget(QWidget* w) { this->contentWidget = w; }
};
class ScrollWidget : public QWidget
{
private:
Widget* widget = nullptr;
QScrollBar* scrollBar = nullptr;
public:
ScrollWidget()
{
setMinimumSize(100, 100);
widget = new Widget;
scrollBar = new QScrollBar(Qt::Vertical);
QHBoxLayout* layout = new QHBoxLayout;
layout->setMargin(0);
layout->setSpacing(0);
layout->addWidget(widget);
layout->addWidget(scrollBar);
setLayout(layout);
}
Widget* getWidget() { return widget; }
QScrollBar* getScrollBar() { return scrollBar; }
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
/*QWebEngineView* view = new QWebEngineView;
view->load(QUrl("https://tabs.ultimate-guitar.com/tab/gloria-gaynor/i-will-survive-ukulele-1947767"));
view->show();*/
QLabel* label = new QLabel(
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
"Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat."
"Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur."
"Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
"Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat."
"Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur."
"Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
);
label->setWordWrap(true);
label->setAlignment(Qt::AlignTop);
ScrollWidget scrollWidget;
scrollWidget.getWidget()->setContentWidget(label);
scrollWidget.show();
return a.exec();
}
Этот код делает не обрабатывать прокрутку (вертикальную или горизонтальную), и я думаю, что был бы другой (лучший) способ сделать то, что я хочу.
Основная цель состояла в том, чтобы отображать больше контента на длинной веб-странице (пример: вкладки гитары) .