My aim is to create an array of command link buttons dynamically on clicking a push button in and then display them all inside a vertical layout inside a QscrollArea. I get the data for the buttons from a database. For this I created a slot for the button and wrote the following code inside the slot function.
QCommandLinkButton *slotButtons[10];
for(int i=0; slotQuery.next(); i++)
{
slotButtons[i] = new QCommandLinkButton;
slotButtons[i]->setText(slotQuery.value(0).toString());
slotButtons[i]->setDescription(slotQuery.value(1).toString());
ui->scrollAreaSlots->layout()->addWidget(slotButtons[i]);
ui->scrollAreaSlots->show();
slotButtons[i]->show();
}
This compiles without errors but the buttons are not visible, even after calling show.
Could anyone tell me where I'm going wrong?
Update: If i remove all the "[i]"s and comment the loop; basically creating just a single command link button, it works perfectly. But it doesn't work for the loop. Is everything right with my looping?
The QScrollArea has one child widget which can contain other widgets.
When a QScrollArea widget is created with Qt Creator's UI designer, Qt Creator creates automatically a widget named scrollAreaWidgetContents. Buttons are then added to that widget's layout, which is not created automatically. The layout is created in the following code which also add the buttons:
QCommandLinkButton *slotButtons[10];
QVBoxLayout* layout = new QVBoxLayout(ui->scrollAreaWidgetContents);
for(int i=0; slotQuery.next(); i++)
{
slotButtons[i] = new QCommandLinkButton;
slotButtons[i]->setText(slotQuery.value(0).toString());
slotButtons[i]->setDescription(slotQuery.value(1).toString());
ui->scrollAreaWidgetContents->layout()->addWidget(slotButtons[i]);
}
Try to add the following line (before adding the button to the scroll area)
slotButtons[i]->setVisible(true);
This should make sure that button itself is visible when you add it to the scrool area.
Related
If I create and show a top-level QWidget, drag it to a new position on the desktop, then call widget->hide() followed by widget::show(), it generally reappears in a different place from where it was previously.
I can add code to the subclass which saves and restores its geometry, but I'm wondering if there's an in-Qt system for giving hints to the window manager as to where the widget should appear when shown.
Is there a nice way to do this?
I create a "Qt Widgets Application" and drag in a QPushButton, then add the following code:
void Widget::on_pushButton_clicked()
{
hide();
show();
}
Run the program, drag the widget to a new position, and then click on the button and I find that the widget is still in the same place, not like you said
I have created widgets from code for the first time instead of through the creator. It reads in a text file and fills a QScrollArea widget with the material name (checkbox to enable it or not) and the thickness lineEdit, as seen in the picture. The number of widgets made corresponds to the number of entries in the text file.
I create the pushbuttons and checkboxes using the following function:
void overlayers::newField(QString material, bool checked, double thickness)
{
//Create new checkbox.
QCheckBox * checkbox = new QCheckBox(material);
checkbox->setChecked(checked);
//Create new field.
QLineEdit * line = new QLineEdit(QString::number(thickness));
//Create horizontal layout.
QHBoxLayout * layout = new QHBoxLayout();
layout->addWidget(checkbox,Qt::AlignLeft);
layout->addWidget(line,Qt::AlignRight);
//Add horizontal layout to vertical.
scrollLayout->addLayout(layout);
}
Where QVBoxLayout scrollLayout is defined in the class header.
I realised after doing the whole dialog box, I have to get the values from the fields and the checkbox flags to save the data but I have no idea how to iterate through all the checkboxes and lineEdits to get the text() and checkedState() variables!
I feel I have programmed this incorrectly and that there is a better way to do it. Is it possible to do this with how I have done it? If not, how would a better programmer do it re: creating the variable number of widgets and being able to access their data? A QVector sort of thing?
Thanks heaps!
http://i.stack.imgur.com/NKJVW.png
How can i show two windows at same time ? Well i have a mainwindow ,then when i press a button i load a plugin which creates and return a qwidget, and set it as central widget setCentralWidget() ,but my app crashes .If i'm not wrong this anyway will show just one window at same time .
Code from plugin :
QWidget* PlPlugin::initPltter() {
plotter = new QWidget();
plotter->resize(200,300);
plotter->setWindowTitle("mypl");
plotter->show();
return plotter;
}
In my app i'm doing :
setCentralWidget(plotter->initPlotter());
Try not resizing or showing it. Its unnecessary if its going to become a central widget of the mainwindow.
Also, from your example there is nothing suggesting that you are trying to show two windows. What you are doing is creating another widget and setting it as a child of the main window. Do one or the other: Create the widget and show it directly, or create it and parent it under another.
Your MainWindow child shown in main.cpp, for second widget do this:
QWidget* PlPlugin::initPltter() {
plotter = new QWidget();
plotter->resize(200,300);
plotter->setWindowTitle("mypl");
plotter->show();
return plotter;
}
And don't do this
setCentralWidget(plotter->initPlotter());
It allow you to get MainWindow and widget in separate 'window'
I want to create a dock widget with a custom title widget. That custom title widget has my own icons (maximize, minimize, close etc).
Source code is simply like that:
QDockWidget *dock = new QDockWidget("name", parent);
MyDockTitle * titleWidget = new MyDockTitle(dock);
dock->setTitleBarWidget(titleWidget);
When I run the program, dock widget is shown appropriately but unfortunately I can not move the dock widget (it is in floating state). What can be the problem?
P.S. When I dont use custom title widget, I can move dock widget.
Thanks...
The Qt documentation of setTitleBarWidget() says:
Mouse events that are not explicitly
handled by the title bar widget must
be ignored by calling
QMouseEvent::ignore(). These events
then propagate to the QDockWidget
parent, which handles them in the
usual manner, moving when the title
bar is dragged, docking and undocking
when it is double-clicked, etc.
So I guess you need to add some QMouseEvent::ignore() calls to your MyDockTitle class.
when i add the widget to the main window, by default the action menu item will be present,
how to remove that?
menuBar()->setVisible(false);
verAction = new QAction(tr("&Version"),this);
menuBar()->addAction(verAction);
connect(verAction, SIGNAL(triggered()),this, SLOT(displayVersion()));
displayAction = new QAction(tr("&Display"),this);
menuBar()->addAction(displayAction);
connect(displayAction, SIGNAL(triggered()),this, SLOT(displayMessage()));
exitAction = new QAction(tr("&Exit"),this);
menuBar()->addAction(exitAction);
connect(exitAction, SIGNAL(triggered()),this, SLOT(close()));
Thanks
If you want to hide an QAction and display it when you need it, you can use the setVisible function.
If you want to remove the menu bar from the QMainWindow, you can use the QT_NO_MENUBAR preprocessor to remove all uses of a QMenuBar. If you are not using facilities provided by QMainWindow, maybe you can use a simple QWidget as main window in your application.
[Edit]
If you want to hide QActions at runtime, you will find them as member of the QMainWindow's UI. For example if you have a QAction named actionTest, you will access it like that: this->ui->actionTest->setVisible(false);
I know what you mean... you want to HIDE the DEFAULT CONTEXT MENU "Actions"....
You can do this in the Design section (not in code).
Then you see your Object-Stack on the right side like
MainWindow QMainWindow
centralWidget QWidget
webView QWebView
Now go to the property editor below...search for "contextMenuPolicy" and change it from "DefaultContextMenu" to "NoContextMenu" for every component if necessairy.
In order to remove the default context menu with the label "Actions" the following code may be used:
// Remove context menu from the all widgets.
QWidgetList widgets = QApplication::allWidgets();
QWidget* w=0;
foreach(w,widgets) {
w->setContextMenuPolicy(Qt::NoContextMenu);
}
Essentially, the same as the Joel's answer, but the code version :)
(Code taken from QFriendFeed sample from forum.nokia.com)