I am creating SpinBox and then I am trying to make it visible inside my MainWindow. Is it any way to do it?
Basically I am creating double array of Spinboxes dynamically in loop like this
array=new QSpinBox *[y+1];
and in another loop like this
array[i]=new QSpinBox[x+1];
which works fine. And now I'd like to show it on main window,with geometry set.
When I do
array[i][j].show()
It generates new windows. Is there a way to do it?
Ps. Basically I want to generate Matrix on screen, so when user press button it switch from 2x2 SpinBoxes to 3x3 SpinBoxes. So in
void MainWindow::on_pushButton_clicked()
I want to generate SpinBoxes on MainWindow. \
Thanks for help
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
Im using QListWidget to show names as a type of dropdown when someone
types entries in another QLineEdit field. It hits the database and shows
all possibilities to choose from. As they type, the list changes, so I want
it to delete all entries and re-fill the QListWidget.
When I call the following code, it indeed empties the QListWidget list, but
the screen elements are still visible. Can someone help me figure out why
they arent being removed from the display? Im using Qt 4.8.4. Thank you!
void myClass::clearListWidget()
{
QListWidget * lw = m_ui->db_listWidget;
while(lw->selectedItems().size())
{
delete lw->takeItem(0);
}
lw->update();
lw->repaint();
qApp->processEvents();
}
Use slot void QListWidget::clear() to clear all contents. QListWidget documentation is here.
I am creating a GUI using Qt. Basically it is an interactive map that shows robots moving in an arena in real time and allows the user to interact with the robots(tell them to go/stop) by using the mouse and keyboard keys. I used opengl to create the map itself and everything is working perfectly, I can see the robots moving on the map and I can press different keys on the keyboard and send the actual robots commands.
Now, I need to take this map and make it become a part of a bigger GUI that holds this map along with other objects as well,not all objects are necessarily using opengl. So, by using the Qt creator (designer) I have some dragged/dropped tabs in my GUI and I have also added a "dockwidget" in my GUI. The dockwidget holds in it the interactive map that I had created earlier. Now, however I can no longer send commands using my keyboard to my map. I can still click on different robots on my map and I can see that they get selected and change colors (as I coded it to do) but pressing keys have no corresponding actions(as it has been coded).
This is what the map by itself looks like. http://dl.dropbox.com/u/46437808/collision3.png
This is the map as a docked widget. (Inside the widge,I was able to click on one robot and make it turn yellow) https://www.dropbox.com/s/lpo43rl6z4268im/argHRI.png
So, my question is how do we direct keyboard input to a specific widget in a window when using Qt. From what I read that it might have to do with setting focus policy. So, I tried to set the focuspolicy of my dockwidget to "StrongFocus" (so that it can take keyboard input) in the constructor but that did not help.
Here is the code in which I'm setting my map as the dockwidget and I'm trying to set the focus as well.
#include "ui_arghri.h"
argHRI::argHRI(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::argHRI)
{
ui->setupUi(this);
ui->dockMap->activateWindow();
//ui->dockMap->keyboardGrabber();
//ui->dockMap->grabKeyboard();
ui->dockMap->setFocus();
ui->dockMap->setFocusPolicy(Qt::StrongFocus);
}
argHRI::~argHRI()
{
delete ui;
}
void argHRI::addMap(Map * map)
{
qDebug()<<"argHRI::in AddMap test is "<<map->test;
//ui->dockMap->show();
ui->dockMap->setWidget(map);
}
Add an event filter that handles KeyPress events to your class. There are examples here: http://doc.qt.io/archives/qt-4.7/eventsandfilters.html
Just don't forget to add:
installEventFilter(this);
to the constructor or it won't work otherwise.
In QListView, i'd like to disable mouse drag multiple selection - that is, mous down on a row, drag the mouse down and select the rows below it while dragging.
I'd still like row selection using CTRL-mouse click.
Is that possible?
It seems you've set the list view's selection mode to QAbstractItemView::MultiSelection. Try setting it to QAbstractItemView::ExtendedSelection with:
listView->setSelectionMode( QAbstractItemView::ExtendedSelection );
and see if that helps.
I think the easiest way to do it would be to create a derived class from the QListView and then override its mouseMoveEvent function. This function in the Qt Code for the QListView looks for a dragging state and creates a rectangle. I think something like this may work, but I didn't test it:
void DerivedListView::mouseMoveEvent(QMouseEvent *e) {
if (state() != DragSelectingState)
QListView::mouseMoveEvent(e);
}