2012-04-07 13 views

risposta

7

Immagino tu intenda un widget di linea orizzontale/verticale: è solo un semplice QWidget con un colore di sfondo grigio e l'orizzontale è un altezza fissa (1-3 pixel) e un widget di larghezza in espansione, la verticale è una larghezza fissa che si espande widget di altezza.

codice esempio orizzontale:

QWidget *horizontalLineWidget = new QWidget; 
horizontalLineWidget->setFixedHeight(2); 
horizontalLineWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); 
horizontalLineWidget->setStyleSheet(QString("background-color: #c0c0c0;")); 
3

Si tratta di un QFrame con altezza 3, ombra infossati e la linea di larghezza uguale a 1. si può vedere se esaminare intestazione generato dallo strumento UIC.

3

Partenza QFrame :: setFrameShape(). Per ottenere una linea, utilizzare QFrame :: HLine o QFrame :: VLine come argomento della funzione.

// Create a horizontal line by creating a frame and setting its shape to QFrame::HLine: 
QFrame* hFrame = new QFrame; 
hFrame->setFrameShape(QFrame::HLine); 

// Create a vertical line by creating a frame and setting its shape to QFrame::VLine: 
QFrame* vFrame = new QFrame; 
vFrame->setFrameShape(QFrame::VLine); 
6

In Qt 5.7 il codice generato da Qt Designer per una linea orizzontale (che può essere controllato nel menu con "Form/Visualizza codice ...") è:

QFrame *line; 
line = new QFrame(Form); 
line->setFrameShape(QFrame::HLine); 
line->setFrameShadow(QFrame::Sunken); 

Questa volontà crea le linee che vedi in Qt Designer.

Le risposte attuali non sembrano dare soluzioni di lavoro, ecco un confronto tra tutte le risposte (questa soluzione è la prima linea):

Horizontal lines in Qt

codice completo:

#include <QtWidgets> 

int main(int argc, char **argv) 
{ 
    QApplication app(argc, argv); 

    QWidget widget; 
    auto layout = new QVBoxLayout; 
    widget.setLayout(layout); 
    widget.resize(200, 200); 

    auto lineA = new QFrame; 
    lineA->setFrameShape(QFrame::HLine); 
    lineA->setFrameShadow(QFrame::Sunken); 
    layout->addWidget(lineA); 

    QWidget *lineB = new QWidget; 
    lineB->setFixedHeight(2); 
    lineB->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); 
    lineB->setStyleSheet(QString("background-color: #c0c0c0;")); 
    layout->addWidget(lineB); 

    auto lineC = new QFrame; 
    lineC->setFixedHeight(3); 
    lineC->setFrameShadow(QFrame::Sunken); 
    lineC->setLineWidth(1); 
    layout->addWidget(lineC); 

    QFrame* lineD = new QFrame; 
    lineD->setFrameShape(QFrame::HLine); 
    layout->addWidget(lineD); 

    widget.show(); 
    return app.exec(); 
} 
Problemi correlati