Qt Widget does not show - c++

I create a class which is a subclass of a QWidget used for painting a image, only for painting a image, named ImageWidget.
When I only create a ImageWidget and call ImageWidget.show(), everything is fine. Then I create another QWidget subclass like below. At present, only shows the combox and slider, the image does not show. Could someone help me about it?
MainWindow::MainWindow(QWidget *parent) :
QWidget(parent)
{
imgWidget=new ImageWidget(this);
fractalTypeLabel=new QLabel(tr("Type"));
typeCombo=new QComboBox();
typeCombo->addItem(tr("One"));
typeCombo->addItem(tr("Two"));
scalefactorLabel=new QLabel(tr("Scale Factor"));
scalefactorSlider=new QSlider(Qt::Horizontal);
scalefactorSlider->setTickInterval(1);
scalefactorSlider->setTickPosition(QSlider::TicksBelow);
QVBoxLayout *imageLayout=new QVBoxLayout();
imageLayout->addWidget(imgWidget);
QGridLayout *gridLayout=new QGridLayout;
gridLayout->addWidget(fractalTypeLabel,0,0);
gridLayout->addWidget(typeCombo,0,1);
gridLayout->addWidget(scalefactorLabel,1,0);
gridLayout->addWidget(scalefactorSlider,1,1);
QHBoxLayout *mainLayout=new QHBoxLayout;
mainLayout->addLayout(imageLayout);
mainLayout->addLayout(gridLayout);
this->setLayout(mainLayout);
}
Best Regards,

Most Likely you are not reporting a size for your widget from sizeHint, in your custom subclass implement virtual QSize sizeHint () const and return something. Your widget will probably show. In the absence of sizeHint the system thinks that your widget can be of size 0x0 and eliminates it from the layout. There are other ways around this by playing with the layout settings. It is slightly non-intuitive ... . You might want to implement some of the other size related functions in from QWidget

Related

How do I make a QWidget automatically scale vertically to it's contents?

I've made a QT Designer Form called DropDownMenu. Essentially it is just a QWidget with a QVBoxLayout inside of it.
DropDownMenu has a function that pragmatically adds buttons to it.
QPushButton* DropDownMenu::AddButton(
const QString& text)
{
QPushButton* new_button = new QPushButton(text, this);
m_ui->LayoutManager->addWidget(new_button);
return new_button;
}
I then add a QWidget to my MainWindow inside of QT Designer & promote this widget to DropDownMenu. Then I add buttons to this new QWidget using the AddButton function.
The end result looks like this...
I want to make it so the containers scale according to how many buttons or widgets are placed inside of the layout, but they seem to just get squeezed together so that they fit the parents default size.
How can I make it so the parent scales to the size of all it's children?

Change QT layout background

Since QVBoxLayout has no a setStylesheet method, I thought this would made the trick:
QWidget *window = new QWidget(this);
window->setStyleSheet("background-image:url(:/images/sky.jpg);font-size:18px;");
QVBoxLayout * layout = new QVBoxLayout(window);
layout->addWidget(widg1);
layout->addWidget(widg2);
setLayout(layout);
Sadly, only a small rectangle of background image appears, not covering entire window. How could I do it?
You can set stylesheet to the central widget of your main window. In the example you can put window into some other layout.

How to create QToolBar in QWidget?

I am trying to add a QToolBar in a QWidget. But I want its functionality to work as if it was a QMainWindow.
Apparently I can not create QToolBar in a QWidget, and using setAllowedAreas does not work with QWidget : it only works with QMainWindow. Also, my QWidget is in a QMainWindow.
How can I create a QToolBar for my widget?
The allowedAreas property only works when the toolbar is the child of a QMainWindow. You can add the toolbar to a layout, but it won't be movable by the user. You can still relocate it programmatically, however.
To add it to a layout for a fictional class inheriting QWidget:
void SomeWidget::setupWidgetUi()
{
toolLayout = new QBoxLayout(QBoxLayout::TopToBottom, this);
//set margins to zero so the toolbar touches the widget's edges
toolLayout->setContentsMargins(0, 0, 0, 0);
toolbar = new QToolBar;
toolLayout->addWidget(toolbar);
//use a different layout for the contents so it has normal margins
contentsLayout = new ...
toolLayout->addLayout(contentsLayout);
//more initialization here
}
Changing the toolbar's orientation requires the additional step of calling setDirection on the toolbarLayout, e.g.:
toolbar->setOrientation(Qt::Vertical);
toolbarLayout->setDirection(QBoxLayout::LeftToRight);
//the toolbar is now on the left side of the widget, oriented vertically
QToolBar is a widget. That's why, you can add a QToolBar to any other widget by calling addWidget for layout or by setting the QToolBar parent to your widget.
As you can see in documentation of QToolBar setAllowedAreas method:
This property holds areas where the toolbar may be placed.
The default is Qt::AllToolBarAreas.
This property only makes sense if the toolbar is in a QMainWindow.
That's why it is impossible to use setAllowedAreas if toolbar is not in QMainWindow.
As far as I know, the only way to properly use the toolbar is with the QMainWindow.
If you want to use the full functionality of the toolbar, create a mainwindow with the window flag Widget. This way you can add it inside some other widget without having it displayed as a new window:
class MyWidget : QMainWindow
{
public:
MyWidget(QWidget *parent);
//...
void addToolbar(QToolBar *toolbar);
private:
QMainWindow *subMW;
}
MyWidget::MyWidget(QWidget *parent)
QMainWindow(parent)
{
subMW = new QMainWindow(this, Qt::Widget);//this is the important part. You will have a mainwindow inside your mainwindow
setCentralWidget(QWidget *parent);
}
void MyWidget::addToolbar(QToolBar *toolbar)
{
subMW->addToolBar(toolbar);
}

Can't add my Widget to a layout

Why can't I add my widget to the layout? It is like the example for adding Widgets...
My aim is to draw into a small QWidget, whichs purpose is only to hold that painted element.
void Note::paintEvent(QPaintEvent *event)
{
paintExample = new(QWidget);
paintExample->setGeometry(500,500,500,500);
paintExample->setStyleSheet("background:yellow");
QImage *arrowImg = new QImage(100,100,QImage::Format_RGB32);
QHBoxLayout *lay = new QHBoxLayout;
lay->addWidget(arrowImg); //Error: no matching function for call to 'QHBoxLayout::addWidget(QImage*&)'
paintExample->setLayout(lay);
QPainter painter(arrowImg); //Must paint on QImage/QPixmap/QPicture, QWidget not possible?
if (painter.isActive()){
painter.begin(arrowImg);
painter.setPen(Qt::black);
QRect rect = QRect(50,25,60,40);
painter.drawRect(rect);
painter.end();
}
paintExample->show();
}
In the class header in private area:
QWidget * paintExample;
Read the documentation carefully:
QHBoxLayout, and any other layout, could only hold items inheriting from QLayoutItem, which are QLayout itself, QSpacerItem and QWidgetItem. Create QWidget and paint on that.
QPainter constructor accepts descendants from QPaintDevice and QWidget is among those, so it's totally possible.
Now, to other issues:
You create parentless objects in paintEvent() without deleting them. Not only this method could be called quite frequently, this approach to painting is not good at all. As I understand Note is a QWidget already, so you're free to paint on it right away, with QPainter painter(this);.
Operations with layout are most definitely not meant to be done in paintEvent() either, as well as showing/hiding widgets. Do this somewhere else, e.g. in your window/dialog constructor.
Why can't I add my widget to the layout?
Because QImage is not a QWidget. That's why. You should probably wrap the image in a QLabel:
QLabel arrowLabel;
arrowLabel.setPixmap(QPixmap::fromImage(*arrowImg));
and pass that to the layout:
lay->addWidget(arrowLabel);

Can you add a toolbar to QDialog?

I'm working on a project that needs to call a modal window with a toolbar to do some work on some data before it's loaded. The reason I need the toolbar is the user has a few different possible options that can be combined.
The obvious choice here is a Modal dialog (which I have working right now). The issue is I want a toolbar. This is a two part question:
Is it possible to add a toolbar to a QDialog? (also is it possible to do this in Qt Designer?)
If 1. is not possible, how can I make a QMainWindow modal?
You can simply use the setMenuBar function of the layout manager that is installed on your QDialog:
myDialog->layout()->setMenuBar(myMenuBar);
If you don't need the built-in drag and drop feature of QMainWindow's toolbars, you can simply add a QToolBar to any layout, including QDialog's layout(). See the DigviJay Patil's answer below for details, which is definitely cleaner conceptually.
Otherwise, please read on.
It is not directly possible to add a QToolBar to a QDialog in the QMainWindow::addToolBar() sense, because QDialog inherits only QWidget and not QMainWindow, as you noted (hence do not have the method addToolBar())
You can't make a QMainWindow modal, but you can insert a QMainWindow in a QDialog this way:
Code:
MyDialog::MyDialog() :
QDialog()
{
QMainWindow * mainWindow = new QMainWindow(); // or your own class
// inheriting QMainWindow
QToolBar * myToolBar = new QToolBar();
mainWindow->addToolBar(myToolBar);
QHBoxLayout * layout = new QHBoxLayout();
layout->addWidget(mainWindow);
setLayout(layout);
}
Indeed, a QMainWindow doesn't necessarily have to be a top-level widget, and you can even insert several QMainWindows as children of a single widget (may not be the wisest choice though, as the user would probably be confused with the separate sets of menu bars, toolbars, dock widgets, etc.).
You can add QToolBar in QDialog. But as a QWidget. Please have a look
MyDialog::MyDialog(QWidget *parent) : QDialog(parent)
{
QVBoxLayout *mainLayout = new QVBoxLayout(this);
QToolBar *toolBar = new QToolBar();
mainLayout->addWidget(toolBar);
QAction *action1 = new QAction("Add", toolBar);
QAction *action1 = new QAction("Del", toolBar);
//Add What you want
}
As QToolBar is child of QWidget we can add it as Widget. Using Layout you can adjust its position. Please check this link http://developer.nokia.com/community/wiki/How_to_use_QToolBar_and_QToolButton_in_Qt