二十一、如何在Item view中设置Icon的大小。
使用setIconSize 即可。
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QTableWidget box;
box.horizontalHeader()->setStretchLastSection(true);
QPixmap pix(50,50);
pix.fill(Qt::red);
QIcon icon(pix);
box.setIconSize(QSize(25,25));
QTableWidgetItem *itemOne = new QTableWidgetItem("one");
QTableWidgetItem *itemTwo = new QTableWidgetItem("two");
QTableWidgetItem *itemThree = new QTableWidgetItem("three");
itemOne->setIcon(icon);
box.setRowCount(5);
box.setColumnCount(5);
box.setItem(0,0,itemOne);
box.setItem(1,0,itemTwo);
box.setItem(2,0,itemThree);
box.show();
return app.exec();
}
二十二、信号和槽的连接方式
信号和槽的连接方式可以是直接连接或者队列方式。直接连接一般在单线程中,槽函数执行完后信号返回。 队列连接用于多线程,在事件循环时候无序的执行连接的槽函数。
二十三、Qt运行时,报类似setGeometry: Unable to set geometry 100x30+1032+685 on QWidgetWindow/‘QDialogClassWindow’
主要原因,父类元件容纳不下子类元件。
二十四、如何从子窗口传递快捷键到父窗口
You can create actions and set a shortcut to them and then add the actions to both the parent and the child as illustrated in the example below:
class Parent : public QWidget {
Q_OBJECT
public:
Parent()
{
auto button = new QPushButton(this);
connect(button, &QPushButton::clicked, this, [this]{
this->showDialog();
});
}
void showDialog()
{
dialog = new QDialog(this);
dialog->setWindowFlags(Qt::Window);
//设置窗口大小,如果设置过小会报错
//etGeometry: Unable to set geometry 100x30+1032+685
//on QWidgetWindow/'QDialogClassWindow'
dialog->setFixedSize(500, 500);
dialog->setWindowTitle("Dialog");
QAction *a = new QAction(this);
a->setShortcut(QKeySequence("Ctrl+X"));
addAction(a);
connect(a, SIGNAL(triggered()), this, SLOT(close()));
dialog->addAction(a);
dialog->show();
}
private:
QDialog *dialog;
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Parent box;
box.setMinimumSize(500, 500);
box.show();
return app.exec();
}
二十四、如何创建没有最大、最小、关闭按钮的对话框QDialog
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QDialog box(0, Qt::WindowTitleHint);
box.show();
return app.exec();
}
二十五、查看app支持图片类型种类
QImageReader::supportedImageFormats()
二十六、如何解决dotted line慢的问题
suggestion that could speed this up for you, is to instead of using a dotted line, use a pattern brush with Dense4Pattern as color for the pen and draw a solid line of width 0. This will give you the same result visually, but will use a faster path through the paint engine. Another alternative is to use an alpha color on the pen instead of a dotted line. This will be visually different, but usually looks as nice.
二十七、用QPainter绘制有外轮廓的文字
void Widget::paintEvent(QPaintEvent *event)
{
QFont serifFont("Times", 40, QFont::Bold);
QPainter painter(this);
QPainterPath path;
path.addText(QPoint(10, 100), serifFont, "Hello");
QPen pen(Qt::green, 1, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin);
painter.setPen(pen);
painter.setBrush(Qt::blue);
painter.drawPath(path);
}
二十八、如何改变QCombobox下拉列表宽度
重写showPopup
class ComboBox : public QComboBox {
public:
ComboBox()
{
addItem("one");
addItem("some more text");
}
void showPopup() override
{
view()->setFixedWidth(400);
QComboBox::showPopup();
}
};
二十九、在QTreeWidget添加选择项
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QTreeWidget tree(0);
tree.setColumnCount(1);
QTreeWidgetItem *itemOne = new QTreeWidgetItem(&tree);
itemOne->setFlags(itemOne->flags()|Qt::ItemIsUserCheckable);
itemOne->setText(0,"Item one");
itemOne->setCheckState(0, Qt::Checked);
tree.show();
return app.exec();
}
三十、如何在QTableWidget 中只产生doubleClicked信号,阻止clicked()信号。
class TableWidget : public QTableWidget {
Q_OBJECT
public:
TableWidget()
{
ignoreNextRelease = false;
timer = new QTimer(this);
setColumnCount(3);
setRowCount(3);
connect(this, SIGNAL(cellClicked (int, int)), this, SLOT(testSlot()));
connect(this, SIGNAL(cellDoubleClicked (int, int)), this, SLOT(testSlot2()));
connect(timer, SIGNAL(timeout()), this, SLOT(emitClicked()));
}
void mouseReleaseEvent(QMouseEvent *event)
{
if (!ignoreNextRelease) {
timer->start(QApplication::doubleClickInterval());
blockSignals(true);
QTableWidget::mouseReleaseEvent(event);
blockSignals(false);
}
ignoreNextRelease = false;
}
void mouseDoubleClickEvent(QMouseEvent *event)
{
ignoreNextRelease = true;
timer->stop();
QTableWidget::mouseDoubleClickEvent(event);
}
public slots:
void testSlot()
{
qDebug("cellClicked() was emitted");
}
void testSlot2()
{
qDebug("cellDoubleClicked was emitted");
}
void emitClicked()
{
emit cellClicked(currentRow(), currentColumn());
timer->stop();
}
private:
QTimer *timer;
bool ignoreNextRelease;
};