| Name | Math | Physics | Average grade |
| | | | |
| | | | |
| | | | |
| | | | |
| | | | |
| | | | |
the column "math" and "physics" is for user to put number in, the column "average" CAN NOT be put value, it is only for showing average value after user put "math" and "physics" grade. How can i disable editing but still show calculated value in column "average" ?
I want to write content to several rows, but my code does not work
ui->tableWidget->setColumnCount(4);
ui->tableWidget->setRowCount(8);
ui->tableWidget->setSortingEnabled(false);
QTableWidgetItem *newItem = new QTableWidgetItem(tr("Hello World"));
for (int i=0; i< ui->tableWidget->rowCount(); i++)
ui->tableWidget->setItem(i, 0, newItem);
There is a message like this
QTableWidget: cannot insert an item that is already owned by another QTableWidget
I still don't understand because i am a newbie with QT. Please help me, many thanks ! :)
Code updated:
for (int i = 0; i < ui.tableWidget->rowCount(); i++) {
QTableWidgetItem *icon_item = new QTableWidgetItem;
QIcon icon(":/myproject/icon/test.png");
icon_item->setIcon(icon);
ui.tableWidget->setItem(i, 0, icon_item);
}
for (int j = 0; j < ui.tableWidget->rowCount(); j++) {
ui.tableWidget->item(j, 3)->setFlags(ui.tableWidget->item(j, 3)->flags() & ~Qt::ItemIsEditable);
}
You can use QTableWidgetItem::setFlags(). To keep the other default flags, but remove Qt::ItemIsEditable:
item->setFlags(item->flags() & ~Qt::ItemIsEditable);
You cannot set the same QTableWidgetItem instance to multiple cells. To get you started:
for (...) {
QTableWidgetItem *item = new QTableWidgetItem(...);
tableWidget->setItem(...);
}
Related
I have a very large file (around 70GB), and I want to list all strings that appear more than once in the whole file.
I can list all the matches when I specify which string to search in a file, but I want to list all strings that have more than one occurrence.
For example, assuming my file looks like this:
+------+------------------------------------------------------------------+----------------------------------+--+
| HHID | VAL_CD64 | VAL_CD32 | |
+------+------------------------------------------------------------------+----------------------------------+--+
| 203 | 8c5bfd9b6755ffcdb85dc52a701120e0876640b69b2df0a314dc9e7c2f8f58a5 | 373aeda34c0b4ab91a02ecf55af58e15 | |
| 7AB | f6c581becbac4ec1291dc4b9ce566334b1cb2c85e234e489e7fd5e1393bd8751 | 2c4f97a04f02db5a36a85f48dab39b5b | |
| 7AB | abad845107a699f5f99575f8ed43e0440d87a8fc7229c1a1db67793561f0f1c3 | 2111293e946703652070968b224875c9 | |
| 348 | 25c7cf022e6651394fa5876814a05b8e593d8c7f29846117b8718c3dd951e496 | 5c80a555fcda02d028fc60afa29c4a40 | |
| 348 | 67d9c0a4bb98900809bcfab1f50bef72b30886a7b48ff0e9eccf951ef06542f9 | 6c10cd11b805fa57d2ca36df91654576 | |
| 348 | 05f1e412e7765c4b54a9acfd70741af545564f6fdfe48b073bfd3114640f5e37 | 6040b29107adf1a41c4f5964e0ff6dcb | |
| 4D3 | 3e8da3d63c51434bcd368d6829c7cee490170afc32b5137be8e93e7d02315636 | 71a91c4768bd314f3c9dc74e9c7937e8 | |
+------+------------------------------------------------------------------+----------------------------------+--+
And I want to list only records which have HHID more than once, i.e, 7AB and 348.
Any idea how can I implement this?
awk to the rescue:
awk -F'[ |]+' '
$2 ~ /^[[:alnum:]]+$/ { count[$2]++ }
END {
for (hhid in count) {
if (count[hhid] >= 2) {
print hhid
}
}
}
' file
-F'[ |]+' sets the field separator.
$2 ~ /^[[:alnum:]]+$/ filters out the header and horizontal lines.
count[$2]++ increases the value at $2, the string we’re counting. On the first occurrence this initialises the value to 1. On the second occurrence it increases it to 2, and so on.
END is run after all lines have been processed.
for (hhid in count) iterates over the strings in count.
if (count[hhid] >= 2) skips any <2 counts.
print hhid prints the string.
Table is the following:
CREATE TABLE UserLog(uid TEXT, clicks INT, lang TEXT)
Where uid field should be unique.
Here is some sample data:
| uid | clicks | lang |
----------------------------------------
| "898187354" | 4 | "ru" |
| "898187354" | 4 | "ru" |
| "123456789" | 1 | <null> |
| "123456789" | 10 | "en" |
| "140922382" | 13 | <null> |
As you can see, I have multiple rows with where the uid field is now duplicated. I would like for those rows to be merged in a following way:
clicks fields are added, and lang fields are updated if their previous value was null.
For the data shown above, it would look something like this:
| uid | clicks | lang |
---------------------------------------
| "898187354" | 8 | "ru" |
| "123456789" | 11 | "en" |
| "140922382" | 13 | <null> |
It seems that I can find many ways to simply delete duplicate data, which I do not necessarily want to do. I'm unsure how I can introduce logic in SQL statements that does this.
First update:
update userlog
set
clicks = (select sum(u.clicks) from userlog u where u.uid = userlog.uid),
lang = (select max(u.lang) from userlog u where u.uid = userlog.uid)
where not exists (
select 1 from userlog u
where u.uid = userlog.uid and u.rowid < userlog.rowid
);
and then delete the duplicate rows that are not needed:
delete from userlog
where exists (
select 1 from userlog u
where u.uid = userlog.uid and u.rowid < userlog.rowid
);
I found this algorithm to check if two polygons intersect:
https://rbrundritt.wordpress.com/2008/10/20/determine-if-two-polygons-overlap/#comment-6287
//poly1 and poly2 are arrays of VELatlongs that represent polygons
function ArePolygonsOverlapped(poly1, poly2)
{
if(poly1.length >= 3 && poly2.length >= 3)
{
//close polygons
poly1.push(poly1[0]);
poly2.push(poly2[0]);
for(var i = 0; i < poly1.length-1;i++)
{
for(var k = 0; k < poly2.length-1; k++)
{
if(SimplePolylineIntersection(poly1[i],poly1[i+1],poly2[k],poly2[k+1])!=null)
return true;
}
}
return false;
}
return null;
}
function SimplePolylineIntersection(latlong1,latlong2,latlong3,latlong4)
{
//Line segment 1 (p1, p2)
var A1 = latlong2.Latitude - latlong1.Latitude;
var B1 = latlong1.Longitude - latlong2.Longitude;
var C1 = A1*latlong1.Longitude + B1*latlong1.Latitude;
//Line segment 2 (p3, p4)
var A2 = latlong4.Latitude - latlong3.Latitude;
var B2 = latlong3.Longitude - latlong4.Longitude;
var C2 = A2*latlong3.Longitude + B2*latlong3.Latitude;
var determinate = A1*B2 - A2*B1;
var intersection;
if(determinate != 0)
{
var x = (B2*C1 - B1*C2)/determinate;
var y = (A1*C2 - A2*C1)/determinate;
var intersect = new VELatLong(y,x);
if(inBoundedBox(latlong1, latlong2, intersect) &&
inBoundedBox(latlong3, latlong4, intersect))
intersection = intersect;
else
intersection = null;
}
else //lines are parrallel
intersection = null;
return intersection;
}
//latlong1 and latlong2 represent two coordinates that make up the bounded box
//latlong3 is a point that we are checking to see is inside the box
function inBoundedBox(latlong1, latlong2, latlong3)
{
var betweenLats;
var betweenLons;
if(latlong1.Latitude < latlong2.Latitude)
betweenLats = (latlong1.Latitude <= latlong3.Latitude &&
latlong2.Latitude >= latlong3.Latitude);
else
betweenLats = (latlong1.Latitude >= latlong3.Latitude &&
latlong2.Latitude <= latlong3.Latitude);
if(latlong1.Longitude < latlong2.Longitude)
betweenLons = (latlong1.Longitude <= latlong3.Longitude &&
latlong2.Longitude >= latlong3.Longitude);
else
betweenLons = (latlong1.Longitude >= latlong3.Longitude &&
latlong2.Longitude <= latlong3.Longitude);
return (betweenLats && betweenLons);
}
However, when I was testing this algorithm with these two polygons, Checking on google maps polygon 1 contains polygon 2. But the algorithm is returning false, i.e. these polygons do not overlap. Please suggest how to fix this.
Polygon 1:
————–+——————+——————+
| polygon_id | latitude | longitude |
+————–+——————+——————+
| 158 | 13.1303042583903 | 77.7543640136719 |
| 158 | 13.1420061213258 | 77.6383209228516 |
| 158 | 13.1189362005413 | 77.5209045410156 |
| 158 | 12.9209143604345 | 77.3890686035156 |
| 158 | 12.7970707734707 | 77.4900054931641 |
| 158 | 12.8446071570180 | 77.6403808593750 |
| 158 | 12.8499628062145 | 77.8154754638672 |
| 158 | 12.9436681426755 | 77.8504943847656 |
| 158 | 13.0420208479226 | 77.8154754638672
Polygon 2:
+--------------+------------------+------------------+
| polygon_id | latitude | longitude |
+--------------+------------------+------------------+
| 150 | 12.8969871916682 | 77.5722312927246 |
| 150 | 12.9225875033164 | 77.6108551025391 |
| 150 | 12.8817596199286 | 77.6197814941406 |
| 150 | 12.8887878450392 | 77.5693130493164 |
+--------------+------------------+------------------+
The part about the intersection looks correct to me, but I don't think it is possible to assert that a polygon is included into another polygon so easily. In fact, if we look more carefully at the code, the algorithm is able to say if polygon A is included into polygon B by only looking at a segment of A and a segment of B. It means that, without knowing anything else about the polygons, giving one segment of each polygon is enough to know if these polygons are included one into the other. That is clearly wrong.
I'm parsing a big XML file, with many items. Each item has many categories, which can repeat. Here's a sample XML.
<item>
<category>Category1</category>
<category>Category2</category>
<category>Category3</category>
<category>Category4</category>
<category>Category5</category>
</item>
<item>
<category>Category1</category>
<category>Category2</category>
<category>Category3</category>
<category>Category7</category>
<category>Category9</category>
</item>
Using doctrine to handle the many-to-many relationship described above, I have a sample code like this:
$em = $this->getDoctrine()->getEntityManager();
foreach ($items as $item) {
[...]
$categories = ... //Array with category names, parsed from the XML.
foreach ($categories as $category) {
//This will check if the 'item' entity
//already has a category with that name.
$exists = $entity->getCategories()->exists(function($key, $element) use ($category) {
return $category == $element->getName();
});
if (!$exists) {
//If there's already one on the database, we'll load it.
//Otherwise, we'll save a new Category..
$query = $this->_entityManager->createQueryBuilder();
$query->select('c')
->from("MyBundle:Category, 'c');
->where("c.name = :name")
->setParameter("name", $category);
}
$result = $query->getQuery()->getOneOrNullResult();
if ($result != null) {
$item->addCategory($result);
} else {
$categoryEntity = new Category($category);
$em->persist($categoryEntity);
$item->addCategory($categoryEntity);
}
}
}
}
The thing is: I only flush() the entitymanager when I complete looping through all items. Therefore, $query->getQuery()->getOneOrNullResult() always returns null, leading me to create duplicated categories.
In the XML example above, I have the following:
| item |
| 1 |
| 2 |
| category.id, category.name |
| 1, Category1 |
| 2, Category2 |
| 3, Category3 |
| 4, Category4 |
| 5, Category5 |
| 6, Category1 |
| 7, Category2 |
| 8, Category3 |
| 9, Category7 |
| 10, Category9 |
| item | category |
| 1 | 1 |
| 1 | 2 |
| 1 | 3 |
| 1 | 4 |
| 1 | 5 |
| 2 | 6 |
| 2 | 7 |
| 2 | 8 |
| 2 | 9 |
| 2 | 10 |
I wanted the following:
| item |
| 1 |
| 2 |
| category.id, category.name |
| 1, Category1 |
| 2, Category2 |
| 3, Category3 |
| 4, Category4 |
| 5, Category5 |
| 6, Category7 |
| 7, Category9 |
| item | category |
| 1 | 1 |
| 1 | 2 |
| 1 | 3 |
| 1 | 4 |
| 1 | 5 |
| 2 | 1 |
| 2 | 2 |
| 2 | 3 |
| 2 | 9 |
| 2 | 10 |
Simply adding $em->flush() after $em->persist($categoryEntity) solves it, but I don't want to flush things just yet (or for that matter, flush only a category). There are a lot of unfinished stuff to do and I don't want to interrupt my transaction. I want to still be able to rollback to the very beginning and exclude all unused categories, if I need to (and, obviously, without running additional queries).
My question is: is there a way to access both the database and doctrine's internal entity mapping to retrieve an entity that might or might not have an ID? Or will I have to create this mapping myself, run a DQL and check on my mapping?
Doctrine2 can't do this for you,
but it's pretty easy to store the newly created categories in your loop and check them when you get a MISS from the database.
$_created_categories = array();
if (!$exists) {
// If there's already one on the database, we'll load it.
// Otherwise, we'll save a new Category..
$query = $this->_entityManager->createQueryBuilder();
$query->select('c')
->from("MyBundle:Category, 'c');
->where("c.name = :name")
->setParameter("name", $category);
$result = $query->getQuery()->getOneOrNullResult();
if ($result) {
$item->addCategory($result);
elseif ( isset($_created_categories[$category]) ) {
$item->addCategory($_created_categories[$category]);
} else {
$categoryEntity = new Category($category);
$em->persist($categoryEntity);
$item->addCategory($categoryEntity);
$_created_categories[$category] = $categoryEntity;
}
}
There is no memory overhead to store the new categories entities in the $_created_categories array as all objects are manipuled by reference in PHP.
Currently a student in college, decided to jump ahead of my programming class and have a little fun with pointers. This is supposed to take a specific serial input and change the state of three LED's I have attached to the Teensy++2.0. However it seems to be just giving me back the first input.
http://arduino.cc/en/Serial/ReadBytesUntil
This is my reference for the ReadBytesUntil() The input goes #,#,### (1,1,255 being an example)
I guess basically my question is, does ReadBytesUntil() deal with commas? And if so, whats going on here?
EDIT -- I asked my teacher and even he has no clue why it doesn't work.
char *dataFinder(char *str){
while (*str != ','){
str++;
}
str++;
return str;
}
void inputDecoder(){
str = incomingText;
whichLED = *str;
dataFinder(str);
onoff = *str;
dataFinder(str);
powerLevel = *str;
}
void loop(){
int length;
if (Serial.available() > 0 ){ //this is basically: if something is typed in, do something.
length = Serial.readBytesUntil(13,incomingText, 10); //reads what is typed in, and stores it in incomingVar
incomingText[length]=0; ///swapping out cr with null
inputDecoder();
//ledControl();
Serial.print("Entered:");
//incomingText[9]=0;
Serial.println(incomingText); //Here for testing, to show what values I'm getting back.
Serial.println(whichLED);
Serial.println(onoff);
Serial.println(powerLevel);
}
delay(1000);
}
The str in inputDecoder() is from the global scope and is not the same str in dataFinder(), which has local scope.
Imagine this ASCII picture is the layout of memory:
str
+-----+-----+-----+-----+ +-----+-----+-----+-----+-----+-----+-----+-----+
| * | | | | ... | 1 | , | 1 | , | 2 | 5 | 5 | \n |
+--|--+-----+-----+-----+ +-----+-----+-----+-----+-----+-----+-----+-----+
|
|
\-----------------------------^
When you pass str to dataFinder() it creates a copy of the pointer, which I'll call str'
str str'
+-----+-----+-----+-----+ +-----+-----+-----+-----+-----+-----+-----+-----+
| * | | * | | ... | 1 | , | 1 | , | 2 | 5 | 5 | \n |
+--|--+-----+--|--+-----+ +-----+-----+-----+-----+-----+-----+-----+-----+
| \-----------------^
|
\-----------------------------^
When dataFinder() increments str it is really altering str'
str str'
+-----+-----+-----+-----+ +-----+-----+-----+-----+-----+-----+-----+-----+
| * | | * | | ... | 1 | , | 1 | , | 2 | 5 | 5 | \n |
+--|--+-----+--|--+-----+ +-----+-----+-----+-----+-----+-----+-----+-----+
| \-----------------------------^
|
\-----------------------------^
Then, when you return to inputDecoder() you dereference str which is still pointing at the start of the string.
You can either assign the value of str' back to the global str using:
str = dataFinder(str);
or change dataFinder() so it does not take an argument, therefore not copying the variable.