wxlistctrl align column image on center - c++

As the title, I can't align an image to the center of a cell in a wxwidget listctrl. As in the attached image you can see the image is everytime aligned to the left. How can I put in the center?
Thanks a lot in advance.
wxBitmap imgCheck(wxT("./immagini/checked.png"), wxBITMAP_TYPE_PNG);
wxBitmap imgUnCheck(wxT("./immagini/unchecked.png"), wxBITMAP_TYPE_PNG);
...
lstUtenti = new wxListCtrl(this,wxID_ANY, wxDefaultPosition, wxSize(780, -1), wxLC_REPORT|wxLC_SINGLE_SEL);
...
wxImageList* imgCheckUncheck = new wxImageList(32,32);
lstUtenti->AssignImageList(imgCheckUncheck, wxIMAGE_LIST_SMALL);
int check = imgCheckUncheck->Add(imgCheck);
int unCheck = imgCheckUncheck->Add(imgUnCheck);
...
lstUtenti->AppendColumn("Notifiche", wxLIST_FORMAT_CENTRE);
lstUtenti->AppendColumn("Eventi", wxLIST_FORMAT_CENTRE);
....
lstUtenti->SetItem(0, 4, "", unCheck);
lstUtenti->SetItem(0, 5, "", check);

Related

How to align QFormLayout in a QGroupBox by Horizontal center

I can`t understand how to align form layout in groupbox by center.
This is how i want it to work. (Correct)
This is how it works now. (Wrong)
Here is the code.
mainWinGroupBox = new QGroupBox(tr("Window Settings"));
mainWinGroupBox->setAlignment(Qt::AlignHCenter);
auto fWinLayout = new QFormLayout;
fWinLayout->setFormAlignment(Qt::AlignHCenter);
fWinLayout->addRow(tr("&Tray:"), trayCheckBox);
WindowWidthSB->setFixedSize(42, 20);
WindowWidthSB->setRange(0, 1920);
WindowWidthSB->setSingleStep(10);
fWinLayout->addRow(tr("&Window Width:"), WindowWidthSB);
WindowHeightSB->setFixedSize(42, 20);
WindowHeightSB->setRange(0, 1080);
WindowHeightSB->setSingleStep(10);
fWinLayout->addRow(tr("&Window Height:"), WindowHeightSB);
mainWinGroupBox->setLayout(fWinLayout);
You can align your labels with this property QFormLayout::labelAlignment.

QtCharts Axis Alignment Center

I'm trying to use QtCharts with X-Axis and Y-Axis in the center of chart.
Therefore the center point will locate in the middle of my QtChart... There are options to alignment axis to the top/bottom or left/right (addAxis(QValueAxis*,Qt::AlignTop)). But, there is no option to align it to the center. When I use AlignCenter as addAxis argument, I get an error.
Any Help?
Tanks.
Edit (Providing code and error):
QChart *BScopeChart = new QChart();
QLineSeries *BScopeSerie = new QLineSeries(this);
BScopeChart->setMargins(QMargins(0, 0, 0, 0));
QValueAxis *BScopeAxisX = new QValueAxis;
BScopeAxisX->setRange(-50, 50);
QValueAxis *BScopeAxisY = new QValueAxis;
BScopeAxisY->setRange(-5, 5);
BScopeChart->addAxis(BScopeAxisY, Qt::AlignLeft); // I need this Line change to something like: BScopeChart->addAxis(BScopeAxisY, Qt::AlignCenter)
BScopeChart->addAxis(BScopeAxisX, Qt::AlignBottom); // I need this Line change to something like: BScopeChart->addAxis(BScopeAxisX, Qt::AlignCenter);
ui.widgetBScopeQChart->setChart(BScopeChart);
and when I change those two lines to:
BScopeChart->addAxis(BScopeAxisY, Qt::AlignCenter);
BScopeChart->addAxis(BScopeAxisX, Qt::AlignCenter);
I face this error:
ErrorScreenshot

How to Stretch Bitmap into Parent Panel (wxWidgets Custom)

Screenshot of Panel and a custom inside of it
I set a wxButton and it loads bitmap,png formats to Panel at right. But image is overfitting. I want to stretc image to Panel when it's loaded by button.
Here is button's function, just in case:
void rrFrame::testClick(wxCommandEvent& event)
{
wxBitmap bmp;
wxString strPath = wxT("img\\img1.png");
bmp.LoadFile(strPath, wxBITMAP_TYPE_ANY);
camFrame_wx->DrawBitmap(bmp, wxPoint(0, 0), false);
//camFrame_wx is the variable name of 'Custom'
}
I suppose i need a stretch or fit function in constructor. How to do this?
I think the easiest way to do this would be to load the image file into a wxImage first, then rescale the wxImage, and finally convert the wxImage to a wxBitmap. Like so:
void rrFrame::testClick(wxCommandEvent& event)
{
wxString strPath = "img\\img1.png";
wxImage im(strPath,wxBITMAP_TYPE_ANY );
wxSize sz = camFrame_wx->GetSize();
im.Rescale(sz.GetWidth(),sz.GetHeight(),wxIMAGE_QUALITY_HIGH );
wxBitmap bmp(im);
camFrame_wx->DrawBitmap(bmp, wxPoint(0, 0), false);
//camFrame_wx is the variable name of 'Custom'
}
Two additional comments:
You shouldn't need the wxT macro for string literals in wxWidgets 3.0 or later.
You can use other options such as wxIMAGE_QUALITY_NEAREST instead of wxIMAGE_QUALITY_HIGH if you need faster rescaling. The full list is available here.

How to keep a Tkinter widget on top of the others

I have some code that moves images left and right but I do not want them to appear on top of the right border, which I draw as a rectangle.
What are the options in Tkinter to keep a widget (in my example a rectangle) on top of some other widgets (in my code a tile, which is an image)?
I am drawing the rectangle and the image on one canvas.
I can image that using two canvas could do the trick, but are there any other options/settings?
Thanks
import Tkinter as tk # for Python2
import PIL.Image, PIL.ImageTk
win = tk.Tk()
#Create a canvas
canvas = tk.Canvas(win, height = 500, width = 500)
#Create a rectangle on the right of the canvas
rect = canvas.create_rectangle(250, 0, 500, 250, width = 2, fill = "red")
#Create an image
SPRITE = PIL.Image.open("sprite.png")
tilePIL = SPRITE.resize((100, 100))
tilePI = PIL.ImageTk.PhotoImage(tilePIL)
tile = canvas.create_image(100, 100, image = tilePI, tags = "a tag")
#Place the canvas
canvas.grid(row = 1, column = 0, rowspan = 5)
#Move the tile to the right.
#The tile will go on top of red rectangle. How to keep the rectangle on top of the tile?
canvas.coords(tile, (300, 100))
canvas.mainloop()
Use tag_raise() method:
canvas.tag_raise(tile)

Qt: TableWidget's ItemAt() acting weirdly

i'm working on a windows application, where in a dialog i query some data from Postgres, and manually show the output in a table widget.
m_ui->tableWidget->setRowCount(joinedData.count());
for(int i=0; i<joinedData.count(); i++) //for each row
{
m_ui->tableWidget->setItem(i, 0, new QTableWidgetItem(joinedData[i].bobin.referenceNumber));
m_ui->tableWidget->setItem(i, 1, new QTableWidgetItem(QString::number(joinedData[i].bobin.width)));
m_ui->tableWidget->setItem(i, 2, new QTableWidgetItem(QString::number(joinedData[i].tolerance.getHole())));
m_ui->tableWidget->setItem(i, 3, new QTableWidgetItem(QString::number(joinedData[i].tolerance.getLessThanZeroFive()))); m_ui->tableWidget->setItem(i, 4, new QTableWidgetItem(QString::number(joinedData[i].tolerance.getZeroFive_to_zeroSeven())));
m_ui->tableWidget->setItem(i, 5, new QTableWidgetItem(QString::number(joinedData[i].tolerance.getZeroFive_to_zeroSeven_repetitive())));
m_ui->tableWidget->setItem(i, 6, new QTableWidgetItem(QString::number(joinedData[i].tolerance.getZeroSeven_to_Three())));
m_ui->tableWidget->setItem(i, 7, new QTableWidgetItem(QString::number(joinedData[i].tolerance.getThree_to_five())));
m_ui->tableWidget->setItem(i, 8, new QTableWidgetItem(QString::number(joinedData[i].tolerance.getMoreThanFive())));
}
Also, based on row and column information, i paint some of these tablewidgetitems to some colors, but i don't think it's relevant.
I reimplemented the QDialog's contextMenuEvent, to obtain the right clicked tableWidgetItem's row and column coordinates:
void BobinFlanView::contextMenuEvent(QContextMenuEvent *event)
{
QMenu menu(m_ui->tableWidget);
//standard actions
menu.addAction(this->markInactiveAction);
menu.addAction(this->markActiveAction);
menu.addSeparator();
menu.addAction(this->exportAction);
menu.addAction(this->exportAllAction);
//obtain the rightClickedItem
QTableWidgetItem* clickedItem = m_ui->tableWidget->itemAt(m_ui->tableWidget->mapFromGlobal(event->globalPos()));
// if it's a colored one, add some more actions
if (clickedItem && clickedItem->column()>1 && clickedItem->row()>0)
{
//this is a property, i'm keeping this for a later use
this->lastRightClickedItem = clickedItem;
//debug purpose:
QMessageBox::information(this, "", QString("clickedItem = %1, %2").arg(clickedItem->row()).arg(clickedItem->column()));
QMessageBox::information(this, "", QString("globalClick = %1, %2\ntransformedPos = %3, %4").arg(event->globalX()).arg(event->globalY())
.arg(m_ui->tableWidget->mapFromGlobal(event->globalPos()).x()).arg(m_ui->tableWidget->mapFromGlobal(event->globalPos()).y()));
menu.addSeparator();
menu.addAction(this->changeSelectedToleranceToUygun);
menu.addAction(this->changeSelectedToleranceToUyar);
menu.addAction(this->changeSelectedToleranceToDurdurUyar);
//... some other irrevelant 'enable/disable' activities
menu.exec(event->globalPos());
}
The problem is, when i right click on the same item i get the same global coordinates, but randomly different row-column information. For instance, the global pos is exactly 600,230 but row-column pair is randomly (5,3) and (4,3). I mean, what?!
Also, when i click to an item from the last to rows (later than 13, i guess) will never go into condition "if (clickedItem && clickedItem->column()>1 && clickedItem->row()>0)", i think it's mainly because 'clickedItem' is null.
I'll be more than glad to share any more information, or even the full cpp-h-ui trio in order to get help.
Thanks a lot.
Try this:
QTableWidgetItem* clickedItem = m_ui->tableWidget->itemAt(event->pos());
The problem is that you are trying to map the global position to the table widget position, without considering the scrollable area. To map the global position into something you can pass to itemAt, use tableWidget->viewport()->mapFromGlobal.
Came here to thank Lukáš Lalinský, as his explanation put an end to half a day of misery for me.
In my case, itemAt() when fed with QDropEvent::pos(), was returning items from a table row, offset by 1.
The reason was that I had a horizontal header, which shrunk the viewport, same with the vertical header, but since it was very narrow, I did not realize it caused an offset too.
The fix was to call QTableWidget's viewport()->mapFromParent( [drop pos] ), as the position was in QTableWidget coordinates already.
Again, thanks A LOT!