MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
m_QPushButton_calibration = new QPushButton("Calibrate", this);
connect(m_QPushButton_calibration, SIGNAL (released()),this, SLOT (handleButton()));
QList<QCameraInfo> l_QListQCameraInfo_available_cameras = QCameraInfo::availableCameras();
m_QCameraViewfinder_viewfinder = new QCameraViewfinder(this);
if (l_QListQCameraInfo_available_cameras.length() >= 2)
{
m_QCamera_required_camera = new QCamera (l_QListQCameraInfo_available_cameras[1]);
m_QCamera_required_camera->setViewfinder(m_QCameraViewfinder_viewfinder);
m_QCamera_required_camera->start ();
}
m_QWidget_central = new QWidget;
m_QGridLayout_main_screen = new QGridLayout;
m_QWidget_central->setStyleSheet ("background-color: green");
m_QWidget_central->setLayout (m_QGridLayout_main_screen);
m_QPushButton_calibration->setStyleSheet ("background-color: pink");
m_QGridLayout_main_screen->addWidget (m_QPushButton_calibration, 0, 0, 1, 1);
m_QCameraViewfinder_viewfinder->setStyleSheet ("background-color: blue");
m_QGridLayout_main_screen->addWidget (m_QCameraViewfinder_viewfinder, 1, 1, 2, 1);
this->setCentralWidget (m_QWidget_central);
m_QCameraViewfinder_viewfinder->show();
}
I have tried this: m_QCameraViewfinder_viewfinder->setStyleSheet ("background-color: blue");
Didn't help.
No camera attached as of now. How to fill QCameraViewFinder with background color?
Output is here:
Related
I want to customize a titlebar:
class TitlebarWidget : public QWidget {
Q_OBJECT
public:
TitlebarWidget(QWidget* parent = nullptr): QWidget(parent) {
setupUi();
}
virtual ~TitlebarWidget();
private:
void setupUi(){
QHBoxLayout* layout = new QHBoxLayout(this);
layout->setSpacing(0);
layout->setContentsMargins(0, 0, 0, 0);
// layout->addSpacerItem(new QSpacerItem(width(), height(),
QSizePolicy::Fixed, QSizePolicy::Fixed));
m_homeButton = new QPushButton(this);
m_homeButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
m_homeButton->setMinimumWidth(50);
m_homeButton->setMaximumWidth(50);
m_homeButton->setText(tr("Home"));
layout->addWidget(m_homeButton);
QLabel* label = new QLabel(this);
label->setText("Titlebar");
layout->addWidget(label, 0, Qt::AlignCenter);
m_synButton = new QPushButton(this);
m_synButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
m_synButton->setMinimumWidth(100);
m_synButton->setMaximumWidth(100);
m_homeButton->setText(tr("Sync"));
}
QPushButton* m_synButton;
QPushButton* m_homeButton;
QPushButton* m_settingButton;
QRCodeWidget* m_synPhoneWidget;
};
The titlebar is as follows:
Home button is covered by the three circles at top-left corner, so I insert an spacer-item at the beginning:
layout->addSpacerItem(new QSpacerItem(width(), height(), QSizePolicy::Fixed, QSizePolicy::Fixed));
And the titlebar is like:
I found the text 'Titlebar' was obviously deviated from the center, but I want keep it central. who can help me with a workaround or a better alternative?
Use layout->addSpacing() instead and addStretch works for me.
I am trying to align QLabels in a simple GridLayout but this doesn't work seems to be a bug in QT 5.9 ?
Here is my snippet, everything is in a QDialog:
MyDialogue::MyDialogue(QWidget *parent) : QDialog(parent) {
QLabel *labelA = new QLabel(); labelA->setFixedSize(100, 25);
QLabel *labelB = new QLabel(); labelB->setFixedSize(100, 25);
QLabel *labelC = new QLabel(); labelC->setFixedSize(100, 25);
QLabel *labelD = new QLabel(); labelD->setFixedSize(100, 25);
labelA->setStyleSheet("background-color:blue");
labelB->setStyleSheet("background-color:yellow");
labelC->setStyleSheet("background-color:purple");
labelD->setStyleSheet("background-color:green");
QGridLayout *layout = new QGridLayout(this);
layout->addWidget(labelA, 1, 1);
layout->addWidget(labelB, 1, 2);
layout->addWidget(labelC, 2, 1, 2, 2);
layout->addWidget(labelD, 3, 1, 3, 2);
}
The result:
Ok I found the solution (my mistake) :
MyDialogue::MyDialogue(QWidget *parent) : QDialog(parent) {
QLabel *labelA = new QLabel();
QLabel *labelB = new QLabel();
QLabel *labelC = new QLabel();
QLabel *labelD = new QLabel();
labelA->setStyleSheet("background-color:blue");
labelB->setStyleSheet("background-color:yellow");
labelC->setStyleSheet("background-color:purple");
labelD->setStyleSheet("background-color:green");
QGridLayout *layout = new QGridLayout(this);
layout->addWidget(labelA, 1, 1);
layout->addWidget(labelB, 1, 2);
layout->addWidget(labelC, 2, 1, 1, 2);
layout->addWidget(labelD, 3, 1, 1, 2);
}
I was incorrectly thinking that for expanding a row along 2 columns (the case of labelC and labelD) I had to write the corrdinates of the starting cell (2,1) the actual position and then the ending cell (2,2). I was misguided by a Java layout manager that worked exactly that way. Just for the record here you just have to indicate the total number of row span and column span which is 2 in my case.
I am new to here.
The ui run well ,but when I click 'okbtn' ...
QObject::connect: No such slot QWidget::makeyourbox() in occQt.cpp:324
And when I click 'cancelbtn', it runs.
Thanks for any responses,
Eason
code:
void occQt::about2() //UI
{
QWidget* pWidget = new QWidget;
QLabel* longlabel = new QLabel(tr("long"));
QLabel* widthlabel = new QLabel(tr("width"));
QLabel* highlabel = new QLabel(tr("high"));
longlineedit = new QLineEdit;
widthlineedit = new QLineEdit;
highlineedit = new QLineEdit;
QPushButton* okbtn = new QPushButton(tr("ok"));
QPushButton* cancelbtn = new QPushButton(tr("cancel"));
QGridLayout* gridlayout = new QGridLayout;
QVBoxLayout* dlglayout = new QVBoxLayout;
QHBoxLayout* btnlayout = new QHBoxLayout;
gridlayout->addWidget(longlabel, 0, 0, 1, 1);
gridlayout->addWidget(widthlabel, 1, 0, 1, 1);
gridlayout->addWidget(highlabel, 2, 0, 1, 1);
gridlayout->addWidget(longlineedit, 0, 1, 1, 3);
gridlayout->addWidget(widthlineedit, 1, 1, 1, 3);
gridlayout->addWidget(highlineedit, 2, 1, 1, 3);
longlineedit->setText("5");
widthlineedit->setText("5");
highlineedit->setText("5");
btnlayout->setSpacing(60);
btnlayout->addWidget(okbtn);
btnlayout->addWidget(cancelbtn);
//pWidget->setLayout(gridlayout);
dlglayout->setMargin(50);
dlglayout->addLayout(gridlayout);
dlglayout->addStretch(40);
dlglayout->addLayout(btnlayout);
pWidget->setLayout(dlglayout);
pWidget->setWindowTitle(tr("Make a Box by custom."));
pWidget->show();
connect(okbtn, SIGNAL(clicked()), pWidget, SLOT(makeyourbox()));
//QObject::connect(okbtn, SIGNAL(clicked()), pWidget, SLOT(close()));
connect(cancelbtn, SIGNAL(clicked()), pWidget, SLOT(close()));
}
void occQt::makeyourbox()
{
QString string_a = longlineedit->text();
eason_a = string_a.toInt();
QString string_b = widthlineedit->text();
eason_b = string_b.toInt();
QString string_c = highlineedit->text();
eason_c = string_c.toInt();
TopoDS_Shape aTopoBox = BRepPrimAPI_MakeBox(eason_a, eason_b, eason_c).Shape();
Handle_AIS_Shape anAisBox = new AIS_Shape(aTopoBox);
anAisBox->SetColor(Quantity_NOC_AZURE);
mContext->Display(anAisBox);
}
When I run the pWidget, click cancelbtn, ui close.
Click okbtn,do nothing..
pWidget is a generic QWidget. It does not contain method/slot makeyourbox().
Your code is faulty.
you should add makeyourbox() method to the subclass of QWidget, and mark it as slot
Double check makeyourbox is defined to be a slot within that class.
My C++ code(use opencv and qt):
face_detect::face_detect(QWidget *parent)
: QWidget(parent)
{
imgwidget = new showwidget("D:\\sample.avi");
imgwidget->resize(QSize(640,480));
setWindowTitle(tr("Face Detection!"));
FaceNumLabel = new QLabel(tr("face_num: "));
num = new QLabel;
num->setFrameStyle(QFrame::Panel | QFrame::Sunken);
ImgSource = new QLabel(tr("image source: "));
VedioBtn = new QPushButton(tr("from vedio"));
LocalImgBtn = new QPushButton(tr("local image"));
LeftLayout = new QVBoxLayout();
LeftLayout->addWidget(imgwidget);
LeftLayout->addWidget(FaceNumLabel);
LeftLayout->addWidget(num);
//LeftLayout->addWidget(ShowImageLabel, 3, 0, 1, 2);
RightLayout = new QVBoxLayout();
RightLayout->addWidget(ImgSource);
RightLayout->addWidget(VedioBtn);
RightLayout->addWidget(LocalImgBtn);
RightLayout->addWidget(ImgSource);
QHBoxLayout *mainLayout = new QHBoxLayout(this);
mainLayout->addLayout(LeftLayout);
mainLayout->addLayout(RightLayout);
mainLayout->setSizeConstraint(QLayout::SetFixedSize);
connect(VedioBtn, SIGNAL(clicked()), this, SLOT(openvedio()));
connect(LocalImgBtn, SIGNAL(clicked()), this, SLOT(localimg()));
}
I call widget.resize() to resize the widget, and then add it to the layout. But when I run the program, the showimgwidget's is not 640*480. Why is that?
This question already has answers here:
how can I fully disable resizing a window including the resize icon when the mouse hovers the border?
(15 answers)
Closed 9 years ago.
I have a QGridLayout inside a QWidget. I keep adding child QGridLayouts to this. I want the QWidget to resize according to the size needed for child layouts but for the user to be unable to resize it.
MainWindow.cpp (MainWindow is inherited from QWidget)
MainWindow::MainWindow(QWidget *parent) :
QWidget(parent)
{
QGridLayout *mainLayout = new QGridLayout();
{
AAController *ac1 = new AAController("Ins1");
AAController *ac2 = new AAController("Ins2");
AAController *ac3 = new AAController("Ins3");
mainLayout->addLayout(ac1, 0, 0);
mainLayout->addLayout(ac2, 0, 1);
mainLayout->addLayout(ac3, 1, 1);
}
setLayout(mainLayout);
}
AAController.cpp (AAController is inherited from QGridLayout)
AAController::AAController(const QString& instrument)
:_instrument(instrument)
{
_lblInstrument = new QLabel(_instrument);
_lblMismatchThreshold = new QLabel("Mismatch threshold : ");
_lblQuoteVolume = new QLabel("Quote volume : ");
_btnUpdateAlgo = new QPushButton("Update Algo");
_spnMismatchThreshold = new QSpinBox();
_spnQuoteVolume = new QSpinBox();
this->addWidget(_lblInstrument, 0, 1, 1, 2, Qt::AlignCenter);
this->addWidget(_lblMismatchThreshold, 1, 0, 1, 2);
this->addWidget(_lblQuoteVolume, 2, 0, 1, 2);
this->addWidget(_btnUpdateAlgo, 3, 2, 1, 2);
this->addWidget(_spnMismatchThreshold, 1, 3);
this->addWidget(_spnQuoteVolume, 2, 3);
setSizeConstraint(SetFixedSize);
}
I get something like this:
But at the moment I can resize this like:
I want to disable such resizing. How do I do this?
Try this in your main window:
this->layout()->setSizeConstraint(QLayout::SetFixedSize);
This is the result i got: